Ejemplo n.º 1
0
        public async Task <AuthenticateResultDto> Authenticate([FromBody] AuthenticateModelInput input)
        {
            var loginResult = await _signInManager.LoginAsync(input.Account, input.Password);


            if (loginResult.Result != LoginResultType.Success)
            {
                throw new UserFriendlyException("登录失败! 用户名或密码错误");
            }

            var result = new AuthenticateResultDto();

            // 使mvc也登录
            if (input.UseCookie)
            {
                await this._signInManager.SignInWithClaimsIdentityAsync(loginResult.ClaimsPrincipal, input.RememberClient);
            }
            if (input.UseToken)
            {
                result.AccessToken          = CreateAccessToken(loginResult.Identity.Claims, expiration);
                result.EncryptedAccessToken = SimpleStringCipher.Instance.Encrypt(result.AccessToken);
                result.ExpireInSeconds      = expiration.TotalSeconds;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <AuthenticateResultDto> RefreshToken()
        {
            var result = new AuthenticateResultDto();

            var userId = _appSession.UserId.Value.ToString();

            var loginResult = await _signInManager.LoginByUserIdAsync(userId);

            result.AccessToken     = CreateAccessToken(loginResult.Identity.Claims, expiration);
            result.ExpireInSeconds = expiration.TotalSeconds;

            return(result);
        }