Solved

Problem With "if" Statement Flow Asp.net

Posted on 2009-06-29
5
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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