Want to re-direct users who didn´t logg on to SignUp.aspx page?

Last post 03-17-2008 12:38 PM by Ajes. 10 replies.

Sort Posts:

  • Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-14-2008, 12:57 PM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    Hi,

    I am using ASP.Net Membership that requries users to logg on if they want

    to use all the functionality on my Footballer site.

    One of the features - allows users to store Footballers that they like in a "UserFavourites" junction table in the database.

    On the next visit to the site,  they can then see view their favourites. The add to favourites button is within the Item Template

    of a datalist that shows all the footballers.

    At the moment if users dont logg on first but click the add to favourites button it causes an exception which I want to fix.

    What I want to happen instead is that users are redirected to SignUp.aspx which asks them to register if  they want to

    use the save to favourties feature.

    Would appreciate if someone can advise me how to do this in C Sharp.

    Many Thanks

    amereto

     

     

     

     

     

     

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-14-2008, 1:17 PM
    Answer
    • Contributor
      2,381 point Contributor
    • amensi
    • Member since 02-13-2006, 7:43 PM
    • Canada
    • Posts 421

    Hi,

    Check before if the user is authenticaded(User.Identity.IsAuthenticated) If not, do a response.redirect.

    Personally, i would not let a anonymous user see the button unless he is authenticaded. That's just me.



    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-14-2008, 1:23 PM
    Answer
    • Participant
      1,123 point Participant
    • RyanSJedi
    • Member since 03-01-2006, 10:38 PM
    • Posts 249

    You need to do an authentication check at some point in your process.  I'd suggest adding something like this to the Add to Favorites button event:

    You'll need to add "using System.Web.Security;" to the code behind our wherever you are putting your code.

    If (User.Identity.IsAuthenticated==false)

    {

       Response.Redirect("/Login.aspx");

    }

    Flames are for BBQ'ing.
    http://weblogs.asp.net/RyanSmith
  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-14-2008, 1:36 PM
    Answer
    • Member
      49 point Member
    • Ajes
    • Member since 10-18-2007, 2:58 AM
    • Posts 34

    Hey  Amereto,

    There are several ways to achive this. Here is a couple i know of.

    1. You can block on page level by adding a couble of  elements to web.config

      <authentication mode="Forms">
          <forms name="appNameAuth" path="/" loginUrl="login.aspx" protection="All" timeout="30">
      </forms>
    
     
    <location path="page.aspx">
          <system.web>
             <authorization>
                <deny users="?"/>
             </authorization>
          </system.web>
       </location>
     This blocks sessions that aren\t logged in from viewing page.aspx. If the try, the are redirected to login.aspx, where you can add login controls. When they use a login control at login.aspx they are again redirected to page.aspx
     

     2. Blocking by checking if user is logged in by calling

    if(User.Identity.IsAuthenticated){
    //allow }
    else{
    //Deny 
    }
     This you could place on your add favorite button.
     
    This is just thought, not sure if you can use them. I you need help on your exception i will gladly help. Just post the exception, and the code that causes it.
     
  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-16-2008, 10:19 AM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    thanks very much for your advice.

    I  want to take this senario one step further and wonder if  you can point me in the right direction.

    What  If a user has logged on and then attempts to add a favurite footaller in the UserFavourties Datatabable that is already there.

    This will also cause an exception.

    The userfavorite table has 2 primary keys which are "UserID" and "FootballerID"

    How in the button click could the ID of the Footballer be checked against the datatable before its stored.

    If it is there already, I want to remind the user that they have already added the player to their favourites.

     

    Regards

     

    amereto

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-16-2008, 3:59 PM
    • Member
      49 point Member
    • Ajes
    • Member since 10-18-2007, 2:58 AM
    • Posts 34

    amereto2k:

    If it is there already, I want to remind the user that they have already added the player to their favourites.

     

    Depends if you want to allow the user to add the favorite.

    1. Let one user have several favorites with same player: You will have to change your primary key or remove it. Then before doing the insert you will have to do a select and check if the player already exists, and do a promt from that result.

    2. Deny users to have same player as several favorites: Catch the primary key exception from the database and promt user and tell him that the favorite already exists.

     

    Hope it helps, and good luck with it.

    Please grant points to the answer you can use?

     

    /Anders

     

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-16-2008, 4:00 PM
    • Member
      49 point Member
    • Ajes
    • Member since 10-18-2007, 2:58 AM
    • Posts 34

    Ajes:
    Please grant points to the answer you can use?

     

    Sorry missed that you already did. Cheers mate Big Smile

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-16-2008, 4:32 PM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    Hi,

     Yes it is the second of your proposals I want to code.

    Could you give me an example of a select clause that  will do the job you mentioned..

     

    Thanks

    Amereto

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-17-2008, 5:58 AM
    • Member
      49 point Member
    • Ajes
    • Member since 10-18-2007, 2:58 AM
    • Posts 34

     

     
        string queryString = "INSERT INTO favorites(playerid, userid) values(1,2)";
        StringBuilder errorMessages = new StringBuilder();
    
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                if(ex.Number == yourErrorNumberForPrimaryKeyException){
                   //promt user
                }
            }
        }
    
     

     This is what i would to. Guess you already have the sql that insert the row, just have to surround it with at try catch block.

     

    /Anders

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-17-2008, 11:22 AM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

     

     Hi Anders,

    My code is below that works currently - not allowing for the exception. 

    Could you brief me on what the stringbuilder part is it and how would you promt the user?

     

     

    string query = string.Format("INSERT INTO UserFavourite (UserID, FootballerID) VALUES('{0}',{1})", userid, fid);

     

    SqlCommand cmd = new SqlCommand(query, con);

    con.Open();

    cmd.ExecuteNonQuery();

    con.Close();

     

    Thanks for your help on this

     

    Amereto

  • Re: Want to re-direct users who didn´t logg on to SignUp.aspx page?

    03-17-2008, 12:38 PM
    • Member
      49 point Member
    • Ajes
    • Member since 10-18-2007, 2:58 AM
    • Posts 34

     

    string query = string.Format("INSERT INTO UserFavourite (UserID, FootballerID) VALUES('{0}',{1})", userid, fid); 
     
    
    SqlCommand cmd = new SqlCommand(query, con); 
    con.Open();
    try{
    cmd.ExecuteNonQuery();
    }
    catch(SqlException ex){
    // Promt user
    }
    
    con.Close();
    
     
    amereto2k:
    Could you brief me on what the stringbuilder part is it and how would you promt the user?

     

    Sorry the StringBuilder part is left over, from a copy/paste. Don't mind that, you don't need it, it was in the sample i copied from used to display the error messages from the SqlException.

     

    string query = string.Format("INSERT INTO UserFavourite (UserID, FootballerID) VALUES('{0}',{1})", userid, fid); 
     
    
    SqlCommand cmd = new SqlCommand(query, con); 
    con.Open();
    try{
    cmd.ExecuteNonQuery();
    }
    catch(SqlException ex){
    //Promt user
    }
    
    con.Close();
    

     

    On pages where i need dynamically to promt user, i place a Label control on the page called lblError, with red bold font. And then when i need to promt the user i simply add some text to the label by lblError.Text = "You have already added the player to your favorites";

    Otherwise you will have to redirect to a other page, but in your case that doesn't make sense in my oppionion.

     

    /Regards Anders

Page 1 of 1 (11 items)