views:

68

answers:

2

I have a login problem.

First i am using SSL while logging.

When i log in, i am creating a cookie like this. when i check if it is secure the answer is yes.

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,                          // version
                                                   UserName.Text,           // user name
                                                   DateTime.Now,               // creation
                                                   DateTime.Now.AddMinutes(60),// Expiration
                                                   false,                      // Persistent 
                                                   role);         // User data

                    // Now encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                    // Create a cookie and add the encrypted ticket to the
                    // cookie as data.
                    HttpCookie authCookie =
                                 new HttpCookie(FormsAuthentication.FormsCookieName,
                                                encryptedTicket);

                    if (authCookie.Secure)
                    {
                        new GUIUtility().LogMessageToFile("The cookie is secure with SSL.");
                        // Add other required code here.
                    }

                    authCookie.Secure = FormsAuthentication.RequireSSL;

                    // Add the cookie to the outgoing cookies collection.
                    HttpContext.Current.Response.Cookies.Add(authCookie);

                    // Redirect the user to the originally requested page 
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text,false));

then this is redirected to the global.asax page which has this code:

string cookieName = FormsAuthentication.FormsCookieName.ToString();
        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        try
        {
            new GUIUtility().LogMessageToFile(cookieName + authCookie.Secure);
        }
        catch (Exception)
        {
            //
        }

here i get the cookieName as ".ASPXAUTH" and authCookie.Secure value as False. Why is this happening i want the authCookie.Secure value to be true here.

Any suggestions?? thanks

my web config has this:

<authentication mode="Forms">
        <forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" slidingExpiration="true" timeout="120" path="/" requireSSL="true" protection="All">
        </forms>
    </authentication>
<httpCookies requireSSL="true"/>
    <authorization>
        <deny users="?"/>
        <!--<allow users="*"/>-->
    </authorization>
+1  A: 

Are you redirecting on log-in to a non-SSL resource? If this is the case, then the cookie you created in the first piece of code shouldn't be used, because it's a secure cookie and hence only applicable to SSL connections (i.e. you explicitly said it shouldn't be sent to non-SSL requests, that's what .Secure does), and hence a new cookie would be created. I would expect it to also not include the ticket value.

In this case, you're going to want to either:

  1. Keep with SSL from the point of being logged in.
  2. Live with the risk of session stealing (there are further means of mitigating this risk).
  3. Use an authentication protocol like Digest or NTLM that allows for challenge-response and for you to more rapidly expire the log-in without the user being pestered (because the browser does the second log-in for you).
Jon Hanna
hi, thanks for the reply.. what i want to do is use SSL for all the pages (all my pages are opening as https). right now as you can see in the login page i get the cookie as secure, but not in the global.asax page and the default.aspx page...i need to pass this same cookie to all the pages and keep it secure.how do i do this... please help me.. any samples
In this case you need to never go to HTTP from the form, but convert any use request to HTTPS, if FormsAuthentication.GetRedirectUrl isn't HTTPS.
Jon Hanna
i am going to Https... as it shows in the URL.. but if you think im going to an http how to change it to https...
so after a lot of debugging i finally come to know that the cookies at both the pages are same but the secure value is true only on the login page. but not on the global.ascx and the default.aspx page. Now i like to go with ur first choice to keep SSL.. please tell me how to do that.. thanks
`UriBuilder ub = FormsAuthentication.GetRedirectUrl(UserName.Text,false); ub.Scheme = https; Response.Redirect(ub.ToString());` should do it. (I prefer to SeeOther when GETting in redirect to a POST, but that's a different matter).If I'm wrong, take a look at what you see with Fiddler, it may give some clues.
Jon Hanna
i get an error message "cannot convert string to uribuilder"so i checked that FormsAuthentication.GetRedirectUrl(UserName.Text,false); is giving me the value as /Default.aspx
Hmm. If it's not absolute it can't be going from HTTPS to HTTP even briefly, so that rules out my reasoning anyway :(
Jon Hanna
thanks for your help any ways...the only solution i found is i have to do it manually...
Hey jon i posted something please give your suggestions..
Tell me, if you run fiddler when it's set to middle-man on the SSL (Tools > Fiddler Options > HTTPS > Decrypt HTTPS Traffic), does the cookie get set with the first response, and does it look correct? What about the subsequent ones?
Jon Hanna
yes everything looks correct the values are getting transfered to an https page
A: 

Restrict the Authentication Cookie-to-HTTPS Connections

Cookies support a "secure" property that determines whether or not browsers should send the cookie back to the server. With the secure property set, the cookie is sent by the browser only to a secure page that is requested using an HTTPS URL.

If you are using .NET Framework version 1.1, set the secure property by using requireSSL="true" on the element as follows:

<forms loginUrl="Secure\Login.aspx"
   requireSSL="true" . . . />

If you are using .NET Framework version 1.0, set the secure property manually in the Application_EndRequest event handler in Global.asax using the following code:

protected void Application_EndRequest(Object sender, EventArgs e) 
 {
string authCookie = FormsAuthentication.FormsCookieName;

foreach (string sCookie in Response.Cookies) 
 {
if (sCookie.Equals(authCookie))
{ 
  // Set the cookie to be secure. Browsers will send the cookie
  // only to pages requested with https
  Response.Cookies[sCookie].Secure = true;
}

} }

so according to me the first option is not working in web config so im doing it manually which is the second option in the code..

Please suggest.