Exemple #1
0
        public UserLoginData GetUserData(HttpContext context)
        {
            UserLoginData   result = null;
            ClaimsPrincipal user   = context.User;

            if (user != null && user.Identity.IsAuthenticated)
            {
                if (user.Identity is ClaimsIdentity identity && identity.Claims.Any(o => o.Type == ClaimTypes.UserData))
                {
                    Claim claim = identity.Claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData);

                    result = JsonConvert.DeserializeObject <UserLoginData>(claim.Value);
                }
            }

            return(result);
        }
Exemple #2
0
        public async Task SignInAsync(HttpContext context, UserLoginData userData, List <string> roles = null)
        {
            string userIdentityJSON = JsonConvert.SerializeObject(userData);
            var    identity         = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

            identity.AddClaim(new Claim(ClaimTypes.Name, userData.FullName));
            identity.AddClaim(new Claim(ClaimTypes.PrimarySid, userData.Id.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.UserData, userIdentityJSON));

            // Add roles
            foreach (var role in roles)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, role));
            }

            // Sign in
            var principal = new ClaimsPrincipal(identity);
            await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
        }