jeudi 21 mai 2015

.NET forms auth cookie not working when deployed to AWS server

I have a .NET MVC app which uses forms authentication that is working fine locally. I'm setting a cookie to maintain the authentication using the following method:

public void SetAuthCookie(UserViewModel user)
{
    var serializeModel = new CustomPrincipalSerializeModel
    {
        Id = user.Id,
        Email = user.Email,
        Name = user.Name
    };
    var serializer = new JavaScriptSerializer();
    var customPrincipal = customPrincipalMapper.Convert(serializeModel);
    var userData = serializer.Serialize(serializeModel);
    var authTicket = new FormsAuthenticationTicket(1, serializeModel.Email, DateTime.Now, DateTime.Now.AddYears(1), false, userData);
    var encTicket = FormsAuthentication.Encrypt(authTicket);
    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
    httpContext.Response.Cookies.Add(authCookie);
}

Again, this works fine locally such that when I debug the app a day after having last logged in, it successfully retrieves the cookie and authenticates me.

I'm having a problem, however, with this functionality when it's deployed to an AWS (Amazon Web Services) server. I can see the cookie exists when I visit the site, but it makes me login each time indicating the code (snippet below) to retrieve the current user was unsuccessful.

var context = ContextHelper.GetHttpContextBase();
var principal = context.User;
if (principal == null || !principal.Identity.IsAuthenticated)
{
    return null;
}
var customPrincipal = (CustomPrincipal)principal;

FWIW, the server is accessed through the instance's public IP address. I'm not sure if I have to set something in IIS (eg. the site's host name), configure something in AWS, or make a change in my app's web.config.

Any ideas? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire