views:

22

answers:

1

Hi,
I have an ASP:NET MVC 2 web site that is on SSL. I want to create a cookie like this:

FormsAuthentication.SetAuthCookie(validatedUser.UserName, false);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, validatedUser.SecureToken, DateTime.Now, DateTime.Now.AddMinutes(10), false, String.Empty);

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);    

But I get an exception, telling: "The application is configured to issue secure cookies. These cookies require the browser to issue the request over SSL (https protocol). However, the current request is not over SSL."

In web.config I have:

<authentication mode="Forms">
    <forms loginUrl="~/Account/LoginError" timeout="2880" requireSSL="true" protection="All"/>
</authentication>    

How can I fix this?

+1  A: 

requireSSL="false" or use http:// to request your site. Note that both are bad idea if you care about security. If you want a secure site leave requireSSL="true" and use https:// to request your site.

Also the SetAuthCookie method already writes the cookie to the response so you don't need the rest:

FormsAuthentication.SetAuthCookie(validatedUser.UserName, false);

is enough. You don't need to worry about FormsAuthenticationTicket and adding the cookie to the response.

Darin Dimitrov
I have requireSSL="true" and use https:// because I need security, but I always get that exception.
dani
Well, the exception you are getting proves that you are not using `https://`. Or maybe there's some firewall or proxy server between the client and the web server which rewrites the request? Is the SSL certificate installed on the web server?
Darin Dimitrov
I found the problem. It is that when I run the web site form VisualStudio it wont work. If I run the site over IIS, it works.
dani