问题描述
我正在.net中用OWIN实现oauth。我能够成功登录到我的提供程序,并使用url中的身份验证代码重定向到我的应用程序。此时一切似乎都不错,但是AuthenticateCoreAsync()方法似乎在我的AuthHandler中没有出现。这是从URL中提取代码以调用令牌端点以获取声明的地方。
有人知道为什么在重定向回我的应用程序时未点击此方法吗?
据我了解,这是流程:
用户点击登录。
应用程序从ApplyResponseChallaengeAsync()重定向到提供程序
提供商使用授权码重定向到应用。
这是我不明白会发生什么的地方
该应用程序处于具有身份验证代码但未命中令牌端点的状态。
我有一个带有[Authorize]标签的自定义终结点,当点击该终结点时,应用程序会提示我登录,然后进入登录过程,然后使用身份验证代码和令牌端点将其留在我的应用程序上没有被击中。
解决方法
我刚刚将owin OpenIdConnect与Apple ID集成在一起,花了很长时间才能解决所有问题。 根据您的描述,我认为您不会处理从Apple返回的代码。如果是这样,您应该在Startup.Auth.cs中使用自定义openId通知处理程序(AuthorizationCodeReceived)进行操作,如下所示:
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = (context) =>
{
var clientToken = JwtTokenGenerator.CreateNewToken();
logger.LogInfo("Apple: clientToken generated");
context.TokenEndpointRequest.ClientSecret = clientToken;
logger.LogInfo("Apple: TokenEndpointRequest ready");
return Task.FromResult(0);
},TokenResponseReceived = (context) =>
{
logger.LogInfo("Apple: TokenResponseReceived");
return Task.FromResult(0);
},SecurityTokenReceived = (context) =>
{
logger.LogInfo("Apple: SecurityTokenReceived");
return Task.FromResult(0);
},SecurityTokenValidated = (context) =>
{
string userID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
logger.LogInfo("Apple: SecurityTokenValidated with userID=" + userID);
return Task.FromResult(0);
},...
};
app.UseOpenIdConnectAuthentication(appleIdOptions);