BUG With FormsAuthentication

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default BUG With FormsAuthentication

    The authentication cookie with custom user is not available or the user data is gone after a redirect.
    In other words all the examples on the net on how to do custom FormsAuthentication don't work!

    Is there a workaround for this?

    Barry
    Software Engineer
    Hogeschool Rotterdam
    The Netherlands
    Faassen, B. Guest

  2. Similar Questions and Discussions

    1. FormsAuthentication
      Hi, i am using forms authentication in an ASP.NET project I am setting the Forms authentication cookie by using:...
    2. FormsAuthentication with non-IE browser
      Hi, I have implemented a SSO solution using FormsAuthentication. FormsAuthentication.GetRedirecturl method does redirect to the requested...
    3. FormsAuthentication with Machine Name
      I had licked this problem once and it resurfaced and won't go away. When I browse to a site with my machine name FormsAuthetication appears to...
    4. FormsAuthentication using xml file
      Hi Andrea, Thanx for the link to the article ...It was really useful...but again i have a question which remains unanswered...Please can u help...
    5. WindowsApplication and FormsAuthentication?
      I have a WebService that is using FormsAuthentication (setup in the web.config file) as follows: <authentication mode="Forms"> <forms...
  3. #2

    Default Re: BUG With FormsAuthentication

    There is no bug this major with formsauthentication, perhaps you can provide us more details/code as to what you are doing and we will try to tell you what you are doing wrong.

    "Faassen, B." <B.Faassen@hro.nl> wrote in message news:e13rNk4eEHA.2916@TK2MSFTNGP12.phx.gbl...
    > The authentication cookie with custom user is not available or the user data is gone after a redirect.
    > In other words all the examples on the net on how to do custom FormsAuthentication don't work!
    >
    > Is there a workaround for this?
    >
    > Barry
    > Software Engineer
    > Hogeschool Rotterdam
    > The Netherlands
    Raterus Guest

  4. #3

    Default Re: BUG With FormsAuthentication


    Hello

    Well I have implemented the IPrincipal and IIdentity interfaces. The
    resulting classes are CustomPrincipal wich has a static Login member and
    uses LDAP to authenticate and retrieves the user info stored in a
    stucture if the login was succesfull. This works fine. No I want to Use
    the Principal wich holds all the struct and other info like roles etc.
    in my ASP.NET application. One way to do this is to generate a ticket
    encrypt it and store the principal in a auth. cookie. Then add this
    cookie to the Coookies collection. From this I do a redirect to the page
    the user requested. In the Global.ASX I have implemented a event member
    AcquireRequestState. In this member I trie to get the auth. cookie I
    just generated and decrypt the ticket and decrypt the principal wich
    should be stored in the ticket. After retrieving the Principal I can set
    it on the HttpContext.Current.User and go on..
    But first of all there is no cookie to get in the Global.ASAX. I never
    get a cookie back except when I use FormsAuthenticate.SetAuthCookie(..)
    in the Login handler
    but I cant use this cookie because its empty.. If I generate the cookie
    on another way the cookie will be lost after Response.Redirect(..)

    I folowed the example of R. Lhotka which has a nice article about
    authentication. I also used examples found in the VS.2003 MSDN docs. I
    also tried some other examples but all give the same result. My cookie
    will be lost somewhere.
    Another trick I tried is to add an extra cookie and first call
    FormsAuthencation.SetAuthCookie(..) and then create a new one add this
    cookie to the collection ... In this case I will get a cookie back but
    then again it is empty..

    Here is my code:

    public static void RedirectFromLoginPage( CustomPrincipal principal )
    {
    string principalText;
    bool persistCookie = false;
    if ( principal != null ) {
    // Encrypt the principal so it can be safely stored
    // in a cookie
    principalText = CustomAuthentication.Encrypt( principal );

    HttpCookie cookie = FormsAuthentication.GetAuthCookie(
    principal.Identity.Name, false );
    FormsAuthenticationTicket ticket =
    FormsAuthentication.Decrypt(cookie.Value);
    FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
    ticket.Version,
    ticket.Name,
    ticket.IssueDate,
    ticket.Expiration,
    ticket.IsPersistent,
    principalText, ticket.CookiePath);
    cookie.Value = FormsAuthentication.Encrypt(newticket);
    cookie.Expires = ticket.Expiration;
    HttpContext.Current.Response.Cookies.Set( cookie );

    HttpContext.Current.Response.Redirect(
    FormsAuthentication.GetRedirectUrl(
    newticket.Name,
    newticket.IsPersistent )
    );
    }

    public static string Encrypt(CustomPrincipal principal)
    {
    MemoryStream buffer;
    IFormatter formatter;
    string principalText = string.Empty;
    if ( principal != null )
    {
    buffer = new MemoryStream();
    formatter = new BinaryFormatter();
    formatter.Serialize(buffer, principal);
    buffer.Position = 0;
    principalText = Convert.ToBase64String( buffer.GetBuffer() );
    }
    return principalText;
    }

    public static CustomPrincipal Decrypt( string encryptedInput )
    {
    CustomPrincipal principal = null;
    MemoryStream buffer = new MemoryStream( Convert.FromBase64String(
    encryptedInput ) );
    BinaryFormatter formatter = new BinaryFormatter();
    principal = (CustomPrincipal)formatter.Deserialize( buffer );
    return principal;

    }

    private void Global_AcquireRequestState(object sender, EventArgs e)
    {
    HttpCookie cookie =
    Request.Cookies.Get(FormsAuthentication.FormsCooki eName);
    if ( cookie != null )
    {
    FormsAuthenticationTicket ticket =
    FormsAuthentication.Decrypt(cookie.Value);
    if ( ticket.Expired )
    {
    FormsAuthentication.SignOut();
    Response.Redirect("login.aspx");
    }
    else
    {
    IPrincipal principal = CustomAuthentication.Decrypt( ticket.UserData );
    HttpContext.Current.User = principal;
    Thread.CurrentPrincipal = HttpContext.Current.User;
    }
    }
    }

    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Barry Faassen Guest

  5. #4

    Default Re: BUG With FormsAuthentication

    If you are having a dissapearing cookie problem, I'd ensure the path of the cookie is set how you would like it. Setting the path to "/" is probably best to make sure this isn't the problem. Also where is this redirect going? Same site, same domain? if you are using cookies it is going to have to be at least the same down to the second level domain. You can't use forms authentication on [url]www.domain1.com[/url] and have it will work when you move over to [url]www.domain2.com[/url].

    Just some thoughts,
    --Michael

    "Barry Faassen" <b.faassen@hro.nl> wrote in message news:Otg3zs7eEHA.2440@tk2msftngp13.phx.gbl...
    >
    > Hello
    >
    > Well I have implemented the IPrincipal and IIdentity interfaces. The
    > resulting classes are CustomPrincipal wich has a static Login member and
    > uses LDAP to authenticate and retrieves the user info stored in a
    > stucture if the login was succesfull. This works fine. No I want to Use
    > the Principal wich holds all the struct and other info like roles etc.
    > in my ASP.NET application. One way to do this is to generate a ticket
    > encrypt it and store the principal in a auth. cookie. Then add this
    > cookie to the Coookies collection. From this I do a redirect to the page
    > the user requested. In the Global.ASX I have implemented a event member
    > AcquireRequestState. In this member I trie to get the auth. cookie I
    > just generated and decrypt the ticket and decrypt the principal wich
    > should be stored in the ticket. After retrieving the Principal I can set
    > it on the HttpContext.Current.User and go on..
    > But first of all there is no cookie to get in the Global.ASAX. I never
    > get a cookie back except when I use FormsAuthenticate.SetAuthCookie(..)
    > in the Login handler
    > but I cant use this cookie because its empty.. If I generate the cookie
    > on another way the cookie will be lost after Response.Redirect(..)
    >
    > I folowed the example of R. Lhotka which has a nice article about
    > authentication. I also used examples found in the VS.2003 MSDN docs. I
    > also tried some other examples but all give the same result. My cookie
    > will be lost somewhere.
    > Another trick I tried is to add an extra cookie and first call
    > FormsAuthencation.SetAuthCookie(..) and then create a new one add this
    > cookie to the collection ... In this case I will get a cookie back but
    > then again it is empty..
    >
    > Here is my code:
    >
    > public static void RedirectFromLoginPage( CustomPrincipal principal )
    > {
    > string principalText;
    > bool persistCookie = false;
    > if ( principal != null ) {
    > // Encrypt the principal so it can be safely stored
    > // in a cookie
    > principalText = CustomAuthentication.Encrypt( principal );
    >
    > HttpCookie cookie = FormsAuthentication.GetAuthCookie(
    > principal.Identity.Name, false );
    > FormsAuthenticationTicket ticket =
    > FormsAuthentication.Decrypt(cookie.Value);
    > FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
    > ticket.Version,
    > ticket.Name,
    > ticket.IssueDate,
    > ticket.Expiration,
    > ticket.IsPersistent,
    > principalText, ticket.CookiePath);
    > cookie.Value = FormsAuthentication.Encrypt(newticket);
    > cookie.Expires = ticket.Expiration;
    > HttpContext.Current.Response.Cookies.Set( cookie );
    >
    > HttpContext.Current.Response.Redirect(
    > FormsAuthentication.GetRedirectUrl(
    > newticket.Name,
    > newticket.IsPersistent )
    > );
    > }
    >
    > public static string Encrypt(CustomPrincipal principal)
    > {
    > MemoryStream buffer;
    > IFormatter formatter;
    > string principalText = string.Empty;
    > if ( principal != null )
    > {
    > buffer = new MemoryStream();
    > formatter = new BinaryFormatter();
    > formatter.Serialize(buffer, principal);
    > buffer.Position = 0;
    > principalText = Convert.ToBase64String( buffer.GetBuffer() );
    > }
    > return principalText;
    > }
    >
    > public static CustomPrincipal Decrypt( string encryptedInput )
    > {
    > CustomPrincipal principal = null;
    > MemoryStream buffer = new MemoryStream( Convert.FromBase64String(
    > encryptedInput ) );
    > BinaryFormatter formatter = new BinaryFormatter();
    > principal = (CustomPrincipal)formatter.Deserialize( buffer );
    > return principal;
    >
    > }
    >
    > private void Global_AcquireRequestState(object sender, EventArgs e)
    > {
    > HttpCookie cookie =
    > Request.Cookies.Get(FormsAuthentication.FormsCooki eName);
    > if ( cookie != null )
    > {
    > FormsAuthenticationTicket ticket =
    > FormsAuthentication.Decrypt(cookie.Value);
    > if ( ticket.Expired )
    > {
    > FormsAuthentication.SignOut();
    > Response.Redirect("login.aspx");
    > }
    > else
    > {
    > IPrincipal principal = CustomAuthentication.Decrypt( ticket.UserData );
    > HttpContext.Current.User = principal;
    > Thread.CurrentPrincipal = HttpContext.Current.User;
    > }
    > }
    > }
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!
    Raterus Guest

  6. #5

    Default Re: BUG With FormsAuthentication

    Watch out for the cookie size of you store custom info inside the FormsAuth
    ticket.
    Check out this posts:

    [url]http://weblogs.asp.net/hernandl/archive/2004/07/30/FormsAuthRolesRev.aspx[/url]
    [url]http://weblogs.asp.net/hernandl/archive/2004/08/05/FormsAuthRoles2.aspx[/url]

    Regards.
    --
    Hernan de Lahitte
    Lagash Systems S.A.
    [url]http://weblogs.asp.net/hernandl[/url]


    This posting is provided "AS IS" with no warranties, and confers no rights.

    "Raterus" <moc.liamtoh@suretar.reverse> wrote in message
    news:O9Qpkz7eEHA.712@TK2MSFTNGP09.phx.gbl...
    If you are having a dissapearing cookie problem, I'd ensure the path of the
    cookie is set how you would like it. Setting the path to "/" is probably
    best to make sure this isn't the problem. Also where is this redirect
    going? Same site, same domain? if you are using cookies it is going to
    have to be at least the same down to the second level domain. You can't use
    forms authentication on [url]www.domain1.com[/url] and have it will work when you move
    over to [url]www.domain2.com[/url].

    Just some thoughts,
    --Michael

    "Barry Faassen" <b.faassen@hro.nl> wrote in message
    news:Otg3zs7eEHA.2440@tk2msftngp13.phx.gbl...
    >
    > Hello
    >
    > Well I have implemented the IPrincipal and IIdentity interfaces. The
    > resulting classes are CustomPrincipal wich has a static Login member and
    > uses LDAP to authenticate and retrieves the user info stored in a
    > stucture if the login was succesfull. This works fine. No I want to Use
    > the Principal wich holds all the struct and other info like roles etc.
    > in my ASP.NET application. One way to do this is to generate a ticket
    > encrypt it and store the principal in a auth. cookie. Then add this
    > cookie to the Coookies collection. From this I do a redirect to the page
    > the user requested. In the Global.ASX I have implemented a event member
    > AcquireRequestState. In this member I trie to get the auth. cookie I
    > just generated and decrypt the ticket and decrypt the principal wich
    > should be stored in the ticket. After retrieving the Principal I can set
    > it on the HttpContext.Current.User and go on..
    > But first of all there is no cookie to get in the Global.ASAX. I never
    > get a cookie back except when I use FormsAuthenticate.SetAuthCookie(..)
    > in the Login handler
    > but I cant use this cookie because its empty.. If I generate the cookie
    > on another way the cookie will be lost after Response.Redirect(..)
    >
    > I folowed the example of R. Lhotka which has a nice article about
    > authentication. I also used examples found in the VS.2003 MSDN docs. I
    > also tried some other examples but all give the same result. My cookie
    > will be lost somewhere.
    > Another trick I tried is to add an extra cookie and first call
    > FormsAuthencation.SetAuthCookie(..) and then create a new one add this
    > cookie to the collection ... In this case I will get a cookie back but
    > then again it is empty..
    >
    > Here is my code:
    >
    > public static void RedirectFromLoginPage( CustomPrincipal principal )
    > {
    > string principalText;
    > bool persistCookie = false;
    > if ( principal != null ) {
    > // Encrypt the principal so it can be safely stored
    > // in a cookie
    > principalText = CustomAuthentication.Encrypt( principal );
    >
    > HttpCookie cookie = FormsAuthentication.GetAuthCookie(
    > principal.Identity.Name, false );
    > FormsAuthenticationTicket ticket =
    > FormsAuthentication.Decrypt(cookie.Value);
    > FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
    > ticket.Version,
    > ticket.Name,
    > ticket.IssueDate,
    > ticket.Expiration,
    > ticket.IsPersistent,
    > principalText, ticket.CookiePath);
    > cookie.Value = FormsAuthentication.Encrypt(newticket);
    > cookie.Expires = ticket.Expiration;
    > HttpContext.Current.Response.Cookies.Set( cookie );
    >
    > HttpContext.Current.Response.Redirect(
    > FormsAuthentication.GetRedirectUrl(
    > newticket.Name,
    > newticket.IsPersistent )
    > );
    > }
    >
    > public static string Encrypt(CustomPrincipal principal)
    > {
    > MemoryStream buffer;
    > IFormatter formatter;
    > string principalText = string.Empty;
    > if ( principal != null )
    > {
    > buffer = new MemoryStream();
    > formatter = new BinaryFormatter();
    > formatter.Serialize(buffer, principal);
    > buffer.Position = 0;
    > principalText = Convert.ToBase64String( buffer.GetBuffer() );
    > }
    > return principalText;
    > }
    >
    > public static CustomPrincipal Decrypt( string encryptedInput )
    > {
    > CustomPrincipal principal = null;
    > MemoryStream buffer = new MemoryStream( Convert.FromBase64String(
    > encryptedInput ) );
    > BinaryFormatter formatter = new BinaryFormatter();
    > principal = (CustomPrincipal)formatter.Deserialize( buffer );
    > return principal;
    >
    > }
    >
    > private void Global_AcquireRequestState(object sender, EventArgs e)
    > {
    > HttpCookie cookie =
    > Request.Cookies.Get(FormsAuthentication.FormsCooki eName);
    > if ( cookie != null )
    > {
    > FormsAuthenticationTicket ticket =
    > FormsAuthentication.Decrypt(cookie.Value);
    > if ( ticket.Expired )
    > {
    > FormsAuthentication.SignOut();
    > Response.Redirect("login.aspx");
    > }
    > else
    > {
    > IPrincipal principal = CustomAuthentication.Decrypt( ticket.UserData );
    > HttpContext.Current.User = principal;
    > Thread.CurrentPrincipal = HttpContext.Current.User;
    > }
    > }
    > }
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Hernan de Lahitte Guest

  7. #6

    Default Re: BUG With FormsAuthentication

    Sorry but I still say there is a BUG in the FormsAuthentication class, because it still works like shit.

    On some browsers it works and on some it doesnt!




    Faassen, B. Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139