问题描述
- 在https:// localhost:5001上运行的apis
- 在https:// localhost:5001上运行的IdentityServer4
- 在http:// localhost:8100上运行的移动应用程序,客户端在使用api。它基于离子+电容器,并使用离子服务启动。
我无法从手机完成登录流程。现在有效的是,移动应用程序调用身份服务器进行授权,身份服务器验证用户,结果应通过重定向返回到移动应用程序。我现在的当前问题是,身份服务器尝试将呼叫重定向到移动应用程序时出现问题。
我的流程如下:
- 移动应用程序使用代码流调用身份服务器授权端点。
- 身份服务器将我重定向到交互式登录页面。
- 我输入电子邮件/密码,然后调用网络api进行身份验证。
- 身份服务器使用令牌和凭据进行呼叫/授权/回调
- 授权/回调应将我重定向到移动应用的redirect_uri (http:// localhost:8100 / authcallback),但是由于出现如下所示的CORS问题,可能是造成此问题的原因?
登录Api端点
[HttpPost("login")]
public async Task<IActionResult> Login(UserResource model) {
var result = await signInManager.PasswordSignInAsync(model.email,model.password,isPersistent: true,lockoutOnFailure: false);
var context = await interaction.GetAuthorizationContextAsync(model.return_url);
if (result.Succeeded) {
// @todo will need to be changed to support multiple organizations
var uo = db.Users.Include(q => q.UserOrganization).Single( q => q.Email == model.email ).UserOrganization.First();
uo.LastLogin = DateTime.UtcNow;
await db.SaveChangesAsync();
// let identity server kNow that we loggedin
await identityEvents.RaiseAsync(new UserLoginSuccessEvent(
model.email,uo.UserId,model.email,clientId: context?.Client.ClientId
));
return Redirect(model.return_url);
/*return Ok( new{
email = model.email,return_url = context.RedirectUri
} );*/
}
// not including an empty object here raises an error,maybe a problem with core3-preview5
await identityEvents.RaiseAsync(new UserLoginFailureEvent(model.email,"invalid credentials",clientId:context?.Client.ClientId));
return NotFound(new {});
}
解决方法
问题实际上是我正在发出从XmlHttpRequest到其他来源(http:// localhost:8100)的重定向,因此chrome不会像那样给我这个错误。
此修复程序在登录控制器中返回Ok()而不是Redirect(),并且在javascript spa客户端中,我使用window.location进行了重定向。