Example #1
0
        public LoginOutput Login(LoginInput input)
        {
            // 验证用户名和密码
            var user = _userRepository.FirstOrDefault(u => u.Account.Equals(input.Account) && u.Password.Equals(input.Password), false) ?? throw Oops.Oh(1000);

            var output = user.Adapt <LoginOutput>();

            // 生成 token
            var jwtSettings    = App.GetOptions <JWTSettingsOptions>();
            var datetimeOffset = DateTimeOffset.Now;

            output.AccessToken = JWTEncryption.Encrypt(jwtSettings.IssuerSigningKey, new Dictionary <string, object>()
            {
                { "UserId", user.Id },       // 存储Id
                { "Account", user.Account }, // 存储用户名

                { JwtRegisteredClaimNames.Iat, datetimeOffset.ToUnixTimeSeconds() },
                { JwtRegisteredClaimNames.Nbf, datetimeOffset.ToUnixTimeSeconds() },
                { JwtRegisteredClaimNames.Exp, DateTimeOffset.Now.AddSeconds(jwtSettings.ExpiredTime.Value * 60).ToUnixTimeSeconds() },
                { JwtRegisteredClaimNames.Iss, jwtSettings.ValidIssuer },
                { JwtRegisteredClaimNames.Aud, jwtSettings.ValidAudience }
            });

            // 设置 Swagger 刷新自动授权
            _httpContextAccessor.SigninToSwagger(output.AccessToken);

            return(output);
        }
Example #2
0
        public LoginOutput Login(LoginInput input)
        {
            // 验证用户名和密码
            var user = _userRepository.FirstOrDefault(u => u.Account.Equals(input.Account) && u.Password.Equals(input.Password), false) ?? throw Oops.Oh(1000);

            var output = user.Adapt <LoginOutput>();

            output.AccessToken = JWTEncryption.Encrypt(new Dictionary <string, object>()
            {
                { "UserId", user.Id },       // 存储Id
                { "Account", user.Account }, // 存储用户名
            });

            // 设置 Swagger 刷新自动授权
            _httpContextAccessor.SigninToSwagger(output.AccessToken);

            return(output);
        }