-3

I am Trying to Design a web App using MVC .. i am trying to access user Login information throughout the application until the user Logout from the application.

please anyone help me... it's most helpful for me if we add session in list variable and pass throughout the application

  • 3
    Possible duplicate of [Session Management in MVC](http://stackoverflow.com/questions/19181085/session-management-in-mvc) – Mairaj Ahmad Feb 21 '17 at 09:52

2 Answers2

0

Visual Studio will do most of the leg work for you on this front, just spin up a new MVC app and add authentication. The asp membership can save you a lot of time here.

To point you in the right direction New Project -> Add New Project Window -> Visual C# -> Web -> Asp .net Application -> MVC

Netferret
  • 604
  • 4
  • 15
0

The best approach to do this is not through session, just use the claims, and it will store the data in session cookies: So you have to add claims at

IdentityModels class

then you have to write the claims in

GenerateUserIdentityAsync method

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            var userId = userIdentity.GetUserId();
            var role = manager.GetRoles(userId);

            // Add custom user claims here
            userIdentity.AddClaims(new[] {
                new Claim("Your claim name", Value that you want to store it)
                });
            return userIdentity;
        }

Then you have to create a new class

public static class IdentityExtensions
    {
        public static Guid GetMyClaim(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("Your claim name");
            return (claim != null) ? claim.Value : string.Empty;
        }
}

After this you can access it like this

User.Identity.GetMyClaim
Amro Mustafa
  • 589
  • 4
  • 15