Solved

Problem With "if" Statement Flow Asp.net

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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.

758 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

20 Experts available now in Live!

Get 1:1 Help Now