J.D*_*Doe 5 authentication backend asp.net-web-api asp.net-core
目前我有一个AuthenticationController,它监听注册后。此注册方法存储用户并创建 jwt 令牌。
[HttpPost("registration")]
public async Task<IActionResult> Registration([FromBody] RegistrationViewModel model)
{
if (!ModelState.IsValid)
return BadRequest(new { code = "ModelNotValid", description = "Model is not valid." });
if (_userService.UserNameTaken(model.UserName))
return BadRequest(new { code = "UserNameTaken", description = $"The User Name
{model.UserName} is taken." });
var identityResult = await _userService.CreateUserAsync(model);
if (!identityResult.Succeeded)
return BadRequest(identityResult.Errors);
var user = _userService.GetUserByUserName(model.UserName);
int validityDurationInHours = 3;
string token = _jwtService.GenerateJwtToken(user, await _userService.GetUserRolesAsync(user),
validityDurationInHours);
return Ok(new { code = "RegistrationSuccess", auth_token = token });
}
Run Code Online (Sandbox Code Playgroud)
但现在我想重构代码,所以会有一个UsersController来保存用户,我不确定是否应该以与现在相同的方法生成 jwt 令牌。
因此,我认为一种方法是,当用户在客户端注册时,它将数据发送到后端,如果正确,则会通知客户端,然后将用户重定向到登录页面。但我觉得没有必要重定向,用户数据是正确的,所以它应该直接获取 jwt 令牌。但我不确定颁发令牌应该由 userscontroller 默认 post 方法完成,这可以保存用户。
我看到的另一种方式是当用户被保存时,后端通知客户端,然后客户端向后端创建一个新请求来颁发令牌。但后来感觉这个请求没有必要。
归档时间: |
|
查看次数: |
389 次 |
最近记录: |