syed mohsin
syed mohsin

Reputation: 2938

Session maintenance in cloud application ASP.Net MVC

I am developing a web application on ASP.Net MVC 4 with razor syntax. I have to deploy it on cloud, probably on Azure. I am quiet confused about the login scheme of MVC. We have to work on multiple schemas so thats why we aren't using the membership provided by ASP.Net.

I know session maintenance and i used it in web forms but session have some serious issues with cloud.

What would be the best method to save usernames and session data?

Upvotes: 2

Views: 542

Answers (1)

Sandrino Di Mattia
Sandrino Di Mattia

Reputation: 24895

I would avoid using the Session State to store user information or even session data, because this makes your application less scalable.

If you want to store a username, displayname, email address, ... I would suggest Claims Based authentication. Brock Allen wrote a great introduction article to get you started: Replacing forms authentication with WIF’s session authentication module (SAM) to enable claims aware identity.

The main idea is that you hand out a cookie (just like with Forms Authentication):

    Claim[] claims = LoadClaimsForUser(username);
    var id = new ClaimsIdentity(claims, "Forms");
    var cp = new ClaimsPrincipal(id);

    var token = new SessionSecurityToken(cp);
    var sam = FederatedAuthentication.SessionAuthenticationModule;
    sam.WriteSessionTokenToCookie(token);

And this cookie represents a ClaimIdentity which can contain one or more claims like email address etc...

private Claim[] LoadClaimsForUser(string username) {
    var claims = new Claim[]
    {
        new Claim(ClaimTypes.Name, username),
        new Claim(ClaimTypes.Email, "[email protected]"),
        new Claim(ClaimTypes.Role, "RoleA"),
        new Claim(ClaimTypes.Role, "RoleB"),
        new Claim(OfficeLocationClaimType, "5W-A1"),
    };
    return claims; }

In terms of session data you might want to consider Windows Azure In-Role Caching or the Windows Azure Caching Service. There's even a Session State Provider which can store the session state in cache: http://msdn.microsoft.com/en-us/library/windowsazure/gg185668.aspx.

But you can easily do this yourself without using the session state by playing with the cache keys, like this:

myCache.Put(user.Id + "_Friends", friendsList);

Upvotes: 2

Related Questions