Пример #1
0
        public async Task <IActionResult> Authenticate([FromBody] UserSignInTransmitModel model)
        {
            object token = await _dataAccess.Authenticate(model);

            if (token != null)
            {
                Response.ContentType = "application/json";
                return(StatusCode(200, token));
            }
            return(StatusCode(404));
        }
Пример #2
0
        public async Task <object> Authenticate(UserSignInTransmitModel model)
        {
            ApplicationUser user = await _userManager.FindByNameAsync(model.UserName);

            if (user != null)
            {
                if (_passwordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) ==
                    PasswordVerificationResult.Success)
                {
                    IList <Claim> userClaims = await _userManager.GetClaimsAsync(user);

                    IEnumerable <Claim> claims = new[]
                    {
                        new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                        new Claim(JwtRegisteredClaimNames.Email, user.Email)
                    }.Union(userClaims);

                    SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
                                                                            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMzRnZzQ1NCIsIm5hbWUiOiJBZG1pbiIsImFkbWluIjp0cnVlfQ.Nmgnewz76zmtuRgrNTpIEaHJueWdulBy5X-Zpg_Lh_s"));
                    SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                    JwtSecurityToken token = new JwtSecurityToken(
                        issuer:
                        "rqDkbIK9YfjFSJYm49x8k8pFkxhX4bJVjEnG059heD6HQrF59F7yVi5V0wJPXBNpTFPmHDmTMoIhYMYnADAqPx",
                        audience:
                        "!de&6Yw8GgcG9!^MQ9Qg4FYv*Ggm8RcpJ93yZUj%z9*6VU62%aXKjU7$ND#*X$jbG@k$CB@7%y*X%qb25r&!#y",
                        claims: claims,
                        expires: DateTime.UtcNow.AddMinutes(60),
                        signingCredentials: creds
                        );

                    var result = (new
                    {
                        token = new JwtSecurityTokenHandler().WriteToken(token),
                        expiration = (60 / 60.0 / 24.0),
                        name = user.UserName,
                        locale = user.Locale
                    });

                    return(result);
                }
            }

            return(null);
        }