Exemplo n.º 1
0
        public async Task <IActionResult> GetUser(GetUserModel model)
        {
            var gotUser = await _user.GetUser(model);

            return(Ok(gotUser));
        }
Exemplo n.º 2
0
        public async Task <CustomResponse <SignInReponseModel> > SignInTheReturnUser(SignInModel model)
        {
            var signedIn = await _signInManager.PasswordSignInAsync(
                model.UserName,
                model.Password,
                model.RememberMe,
                lockoutOnFailure : false);

            #region Validate LogIn

            if (!signedIn.Succeeded)
            {
                _infos.Add("You have entered an invalid username or password.");

                return(new CustomResponse <SignInReponseModel> {
                    Message = _infos
                });
            }

            #endregion

            var user = await _userManager.FindByNameAsync(model.UserName);

            var claims = new[] {
                new Claim(JwtRegisteredClaimNames.Email, user.Email),
                new Claim(JwtRegisteredClaimNames.Sub, user.Id),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
            };

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_options.Value.Key));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var sessionToken = new JwtSecurityToken(_options.Value.Issuer,
                                                    _options.Value.Issuer,
                                                    claims,
                                                    expires: DateTime.UtcNow.AddMinutes(_options.Value.AccessTokenExpiryMins),
                                                    signingCredentials: creds);

            var refreshedToken = _refreshToken.GenerateRefreshToken(new RefreshTokenGetModel {
                UserId     = user.Id,
                ApiVersion = model.ApiVersion,
                AppVersion = model.AppVersion,
                UserAgent  = model.UserAgent
            });

            #region Validate Refresh Token

            if (!refreshedToken.Result.Succeed)
            {
                return(new CustomResponse <SignInReponseModel> {
                    Message = refreshedToken.Result.Message
                });
            }

            #endregion

            var accessToken = new JwtSecurityTokenHandler().WriteToken(sessionToken);

            return(new CustomResponse <SignInReponseModel> {
                Succeed = true,
                Data = new SignInReponseModel {
                    Authorization = new SignInReturnToken {
                        SessionToken = new TokenResource {
                            Token = accessToken,
                            ExpiredAt = sessionToken.ValidTo
                        },
                        RefreshToken = new TokenResource {
                            Token = refreshedToken.Result.Data.Token,
                            ExpiredAt = refreshedToken.Result.Data.ExpiredAt
                        }
                    },
                    User = _user.GetUser(new GetUserModel {
                        UserId = user.Id
                    }).Result.Data
                }
            });
        }