Solved

Problem With "if" Statement Flow Asp.net

Posted on 2009-06-29
5
214 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Binding a check box in an Asp.net DataGrid 4 38
what technologies offer Authentication over Web Services? 4 103
Not showing JavaScript in the list 5 36
Connection String 16 42
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now