コード例 #1
0
        public IActionResult GetToken(string userName, string password)
        {
            var userModel = new JwtUserViewModel()
            {
                Audience   = SysContext.JwtAudience,
                Issuer     = SysContext.JwtIssuer,
                SigningKey = SysContext.JwtSigningKey,
                UserName   = userName,
                Password   = password,
            };
            var service = new AuthValidationService();
            var result  = service.GetAuthToken(userModel);

            return(Json(result));
        }
コード例 #2
0
        public JwtTokenAlertMsg UriToken([FromUri] string userName, [FromUri] string password)
        {
            var userModel = new JwtUserViewModel()
            {
                Audience   = SysContext.JwtAudience,
                Issuer     = SysContext.JwtIssuer,
                SigningKey = SysContext.JwtSigningKey,
                UserName   = userName,
                Password   = password,
            };
            var service = new AuthValidationService();
            var result  = service.GetAuthToken(userModel);

            return(result);
        }
コード例 #3
0
        public JwtTokenAlertMsg PostToken(dynamic obj)
        {
            string userName = Convert.ToString(obj.userName);
            string password = obj.password ?? string.Empty;

            var userModel = new JwtUserViewModel()
            {
                Audience   = SysContext.JwtAudience,
                Issuer     = SysContext.JwtIssuer,
                SigningKey = SysContext.JwtSigningKey,
                UserName   = userName,
                Password   = password,
            };
            var service = new AuthValidationService();
            var result  = service.GetAuthToken(userModel);

            return(result);
        }
コード例 #4
0
        public JwtTokenAlertMsg GetAuthToken(JwtUserViewModel model)
        {
            var username = model.UserName;
            var password = model.Password;
            var identity = GetClaimsIdentity(username, password);

            if (identity == null)
            {
                return(new JwtTokenAlertMsg {
                    statusCode = "400", Message = "Invalid username or password."
                });
            }

            var now    = DateTime.Now;
            var claims = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, username),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(now).ToString(), ClaimValueTypes.Integer64)
            };
            var securityKey        = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(model.SigningKey));
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var jwt = new JwtSecurityToken(
                issuer: model.Issuer,
                audience: model.Audience,
                claims: claims,
                notBefore: now,
                expires: now.Add(model.Expiration),
                signingCredentials: signingCredentials);
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            var response = new JwtTokenAlertMsg
            {
                statusCode   = "200",
                Message      = "登录成功",
                access_token = encodedJwt,
                expires_in   = (int)model.Expiration.TotalSeconds,
            };

            return(response);
        }