Solved

Problem With "if" Statement Flow Asp.net

Posted on 2009-06-29
5
216 Views
Last Modified: 2012-05-07
I have stored subscription expiration dates in a field inside my db. The dates are either 31 days after the date purchased or 1 day after the date purchased. When going to use these dates I am uncertain how to create an "if" statement that makes sure the user cannot login after his expiration date. Below I have only a piece of it that could be user for users who have an expiration date of tomorrow. Can someone add on to this to not let users login if they are past their 31 day subscription? Also is this "if" suppose to go into the Login_click event to work corectly or am I putting it in the wrong place?
//For case user has only 1 day subscription
  if (SubExpirationDate.Date != DateTime.Now.Date)
        {
            Response.Write("Expired");
        }

Open in new window

0
Comment
Question by:npl77
5 Comments
 
LVL 7

Expert Comment

by:urir10
ID: 24740133
Are you storing their expiration date in the database or just the number 31 / 1?
And yes the if can go in the login click even which you will redirect in case its expired.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 24740149
your logic is off.
it should be:
if (SubExpirationDate.Date > DateTime.Now.Date)
        {
//would do Repsonse.Redirect(Aspired.aspx.)
           Response.Write("Expired");
        }
else
Response.Redirect("~/members/home.aspx")

or something to that effect.
0
 

Author Comment

by:npl77
ID: 24740299
Ok, how can I use the login control in asp.net to redirect to a PurchasedPage if the user has logged in successfully and fullfilled the expirationdate? Also I am not sure how/where to set Failure.Text messages stating to the user either his subscription has expired or he has an invalid password. Here is what I have so far Can someone help me fix this thing to work as stated?

The could maybe wrong and definatly not complete(I dont know where to put the invalid password fail text, also am not sure I am using the right event)

One other thing I have put this in my web config...forgot what it was for dunno if it effects this code or not....
<forms name="BMHLogin" loginUrl="Login.aspx" path="/" protection="All" timeout="60" defaultUrl="Purchased.aspx">
</forms>
protected void LoginButton_Click(object sender, EventArgs e)
    {
 
        DataTable dt = BMHAccess.GetUserInfo(login2.UserName);
        if (dt.Rows.Count > 0)
        {
          DateTime exDate=DateTime.Parse(dt.Rows[0]["SubExpirationDate"].ToString());
            if (exDate > DateTime.Now.Date)
        {
          //would do Repsonse.Redirect(Aspired.aspx.)
            Response.Redirect("PurchasedPage.aspx");
           
        }
 
 
            
        }
        else
        {
            login2.FailureText = "Expired";
        
        }
}

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24740639
You should use OnAuthenticate event of Login control and not LoginButton OnClick.

protected void login2_Authenticate(object sender, AuthenticateEventArgs e)
    {
if(Membership.ValidateUser(login2.UserName,login2.Password))
{ //user is authenticated
  DataTable dt = BMHAccess.GetUserInfo(login2.UserName);
        if (dt.Rows.Count > 0)
        {
          DateTime exDate=DateTime.Parse(dt.Rows[0]["SubExpirationDate"].ToString());
            if (exDate.Date > DateTime.Now.Date)
        {      //valid subscription
                e.Authenticated = true;
                 Response.Redirect("PurchasedPage.aspx");          
        }
     else
        {   //Subscription Expired
            login2.FailureText = "Subscription Expired";
            Response.Redirect("Subscribe.aspx");
        }
}
//this else is not mandatory it can be removed
else
{   //Invalid username/password
    e.Authenticated = false;
}
}
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24740654
may be i missed one }
protected void login2_Authenticate(object sender, AuthenticateEventArgs e)
    {
if(Membership.ValidateUser(login2.UserName,login2.Password))
{ //user is authenticated
  DataTable dt = BMHAccess.GetUserInfo(login2.UserName);
        if (dt.Rows.Count > 0)
        {
          DateTime exDate=DateTime.Parse(dt.Rows[0]["SubExpirationDate"].ToString());
            if (exDate.Date > DateTime.Now.Date)
        {      //valid subscription
                e.Authenticated = true;
                 Response.Redirect("PurchasedPage.aspx");          
        }
     else
        {   //Subscription Expired
            login2.FailureText = "Subscription Expired";
            Response.Redirect("Subscribe.aspx");
        }
}
}
//this else is not mandatory it can be removed
else
{   //Invalid username/password
    e.Authenticated = false;
}
}
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question