public ApiResult <TokenResultDto> RefreshToken(string refreshToken) { TokenResultDto tokenResultDto; ApiResult <TokenResultDto> status = new ApiResult <TokenResultDto>(); status.State = false; var refreshTokenState = _refreshTokenService.UpdateRefreshToken(refreshToken); if (refreshTokenState.State) { // 以下變數值應該透過 IConfiguration 取得 var issuer = _configuration["JWT:issuer"].ToString(); //"JwtAuthDemo"; var signKey = _configuration["JWT:signKey"].ToString(); // 請換成至少 16 字元以上的安全亂碼 var expires = Convert.ToInt32(_configuration["JWT:expires"]); // 單位: 分鐘 string UserId = refreshTokenState.Result.Nobr.ToString(); tokenResultDto = new TokenResultDto() { accessToken = JwtHelpers.GenerateToken(issuer, signKey, UserId, expires, _userInfoService.GetApiRoles(UserId), JsonConvert.SerializeObject(_userInfoService.GetUserInfo(UserId))), refreshToken = refreshToken }; status.State = true; status.Result = tokenResultDto; } return(status); }
public ApiResult <TokenResultDto> SignIn(string UserId, string Password) { // 以下變數值應該透過 IConfiguration 取得 var issuer = _configuration["JWT:issuer"].ToString(); //"JwtAuthDemo"; var signKey = _configuration["JWT:signKey"].ToString(); // 請換成至少 16 字元以上的安全亂碼 var expires = Convert.ToInt32(_configuration["JWT:expires"]); // 單位: 分鐘 TokenResultDto tokenResultDto; ApiResult <TokenResultDto> status = new ApiResult <TokenResultDto>(); status.State = false; if (_userValidateService.ValidateUser(UserId, Password)) { var refreshToken = Guid.NewGuid().ToString(); _refreshTokenService.InsertRefreshToken(UserId, refreshToken); tokenResultDto = new TokenResultDto() { accessToken = JwtHelpers.GenerateToken(issuer, signKey, UserId, expires, _userInfoService.GetApiRoles(UserId), JsonConvert.SerializeObject(_userInfoService.GetUserInfo(UserId))), refreshToken = refreshToken }; status.State = true; status.Result = tokenResultDto; } else { status.Message = "帳號密碼輸入錯誤"; } return(status); }
public AccountControllerFixture() { ApiVersion = new ApiVersion(1, 0); MockAccountRepository = new Mock <IAccountRepository>(); HttpContext = new DefaultHttpContext(); Users = new List <GetUserDto> { new GetUserDto { Id = Guid.NewGuid(), FirstName = "Anna", LastName = "Domino", Email = "*****@*****.**", PhoneNumber = "", Roles = new List <string> { "SuperAdmin", "Admin", "Report", "Normal" } }, new GetUserDto { Id = Guid.NewGuid(), FirstName = "Neil", LastName = "Down", Email = "*****@*****.**", PhoneNumber = "", Roles = new List <string> { "Admin" } }, new GetUserDto { Id = Guid.NewGuid(), FirstName = "Mark", LastName = "Ateer", Email = "*****@*****.**", PhoneNumber = "", Roles = new List <string> { "Report", "Normal" } }, new GetUserDto { Id = Guid.NewGuid(), FirstName = "Dave", LastName = "Allippa", Email = "*****@*****.**", PhoneNumber = "", Roles = new List <string> { "Normal" } } }; ValidLoginDto = new LoginDto { Email = "*****@*****.**", Password = "******" }; ValidRegisterAdminDto = new RegisterAdminDto { FirstName = "John", LastName = "Quil", ConfirmPassword = "******", Password = "******", Email = "*****@*****.**" }; ValidRegisterNormalDto = new RegisterNormalDto { FirstName = "Rose", LastName = "Bush", ConfirmPassword = "******", Password = "******", Email = "*****@*****.**", Roles = new List <string> { "Report", "Normal" } }; ValidLoginDto = new LoginDto { Email = "*****@*****.**", Password = "******" }; ValidRefreshDto = new RefreshDto { RefreshToken = "this is refresh token" }; ValidRevokeDto = new RevokeDto { RefreshToken = "this is refresh JWT token" }; SuccessAdminRegisterResultDto = new RegisterResultDto { Status = "Success", Message = "User created successfully, grant Admin access." }; FailedRegisterResultDto = new RegisterResultDto { Status = "Failed", Message = "Failed to create new user." }; SuccessNormalRegisterResultDto = new RegisterResultDto { Status = "Success", Message = $"User created successfully, grant {string.Join(", ", ValidRegisterNormalDto.Roles)} access." }; ValidTokenResultDtoResult = new TokenResultDto { AccessToken = "this will be a JWT access token", RefreshToken = "this will be a JWT refresh token", }; CurrentUserDtoResult = new CurrentUserDto { FirstName = "Simon", LastName = "Sais", Email = "*****@*****.**", Roles = new List <string> { "SuperAdmin", "Admin" }, FullName = "Simon Sais", UserId = Guid.Parse("77d8500b-dd97-4b6d-ce43-08d8aa3916b9") }; }