locked
How to get ApplicationUser object from identity authentication cookie without going to database? RRS feed

  • Question

  • User1884398186 posted

    I am using Webforms with 4.5 .Net Framework. 

    In the website I have, a user is logged into the website using OWIN framework as in code below.

    ///login the user using OWIN framework (not forms authentication)
    var manager = new UserManager();
    ApplicationUser appUser = new ApplicationUser();
    appUser.Email = user.Email;
    appUser.UserName = user.DisplayName;
    appUser.EmailConfirmed = true;
    appUser.Id = user.UserId;
    appUser.PicUrl = user.Userpic;
                
    //sign in the user           
    IdentityHelper.SignIn(manager, appUser, false);//creates the asp.net identity cookie
    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);//redirects to the protected url

    Now in code-behind of some aspx page while the user is logged in, I would like to get the ApplicationUser object without incurring a database trip by using the Identity authentication cookie that was created when user signed in.  I have used the code below, but it always ends up going to the database. 

    Question: How would I get ApplicationUser object for signed in user by using the authentication cookie in code-behind of an aspx ?

    UserManager manager = new UserManager();
    var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
    ApplicationUser appUser = manager.FindByName("abc xyz");//database call to read of AspNetUserLogins

    Wednesday, March 28, 2018 7:33 AM

All replies

  • User283571144 posted

    Hi sun21170,

    According to your description, I suggest you could try to create the ClaimsIdentity class to store the user information into the cookie.

    You could find the user information from user claim as below:

               var ctx = Request.GetOwinContext();
                ClaimsPrincipal user = ctx.Authentication.User;
                IEnumerable<Claim> claims = user.Claims;

    If you are creating users within your system as follows:

    UserManager<applicationuser> userManager = new UserManager<applicationuser>(new UserStore<applicationuser>(new SecurityContext()));
    ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

    You should automatically have some Claims populated relating to you Identity.

    To add customized claims after a user authenticates you can do this as follows:

    var user = userManager.Find(userName, password);
    identity.AddClaim(new Claim("myCustomClaim", "value of claim"));

    The claims are persisted when you call below passing the identity in:

      AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = RememberMe.Checked }, identity);
    

    More detrails, you could refer to below article.

    http://kevin-junghans.blogspot.sg/2013/12/using-claims-in-aspnet-identity.html 

    Result:

    Best Regards,

    Brando

    Thursday, March 29, 2018 8:26 AM
  • User-134105967 posted

    Hi,


      
      You may add user details to claims as shown below,    
             var claims = new List<Claim>
             {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.Sid, user.UserId.ToString()),
                new Claim(ClaimTypes.Role, Role)
             };
             var userIdentity = new ClaimsIdentity(claims, "login");
             ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);


      Then you can retrieve the values from Claims later on like,


             IClaimsPrincipal icp = User as IClaimsPrincipal;  
             IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;

             string userName = ( from c in claimsIdentity.Claims 
                                 where c.ClaimType == "Name" 
                                 select c.Value ).Single();

    Monday, April 2, 2018 1:36 PM