コード例 #1
0
        public void SignInViaCookies(LoggedClaims loggedClaims, bool isPersistent)
        {
            var identity = new ClaimsIdentity(loggedClaims.GetAsClaims(), CookieAuthenticationDefaults.AuthenticationScheme);

            _contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)
                                                     , new AuthenticationProperties()
            {
                IsPersistent = isPersistent
            })
            .Wait();
        }
コード例 #2
0
        public string GenerateToken(LoggedClaims loggedClaims, DateTime?expires)
        {
            var handler = new JwtSecurityTokenHandler();

            var identity   = new ClaimsIdentity(loggedClaims.GetAsClaims(), JwtBearerDefaults.AuthenticationScheme);
            var descriptor = new SecurityTokenDescriptor
            {
                Issuer             = _tokenAuthOptions.Issuer,
                Audience           = _tokenAuthOptions.Audience,
                SigningCredentials = _tokenAuthOptions.SigningCredentials,
                Subject            = identity,
                Expires            = expires
            };

            var securityToken = handler.CreateToken(descriptor);

            return(handler.WriteToken(securityToken));
        }
コード例 #3
0
        public void RefreshCurrentLoggedUserInfo(bool refreshClaims = true)
        {
            if (LoggedClaims == null)//we are not signed in
            {
                return;
            }

            _memoryCache.Remove(GetUserDbInfoCacheKey(LoggedClaims.Login));
            if (refreshClaims)
            {
                var authType = _contextAccessor.HttpContext.User.Identity.AuthenticationType;
                switch (authType)
                {
                case CookieAuthenticationDefaults.AuthenticationScheme:
                    var newClaims = new LoggedClaims(_unitOfWork.Users.GetAccountByIdOrNull(LoggedClaims.UserId));
                    SignInViaCookies(newClaims, true /*TODO: detect if current cookies persistent or not*/);
                    break;

                default:
                    throw new Exception($"RefreshCurrentLoggedUserInfo does not support {authType} authentication");
                }
            }
        }