【发布时间】:2017-08-07 10:38:34
【问题描述】:
场景: Web 应用程序和 Web API 都需要从服务器端进行身份验证和保护。
要求: Web 应用程序为浏览器提供内容,浏览器应该直接调用 Web API(即浏览器到 API)。
问题: 是否可以同时使用令牌对 Web APP 和 API 进行身份验证?
任何示例代码或明确的方向都将受到高度赞赏。
通常 Web 应用程序使用 cookie 进行身份验证,API 使用令牌进行身份验证。有一些可用的示例项目 here 但它们要么是浏览器到 API(基于 SPA 令牌),要么是服务器端 Web 应用程序从服务器到服务器调用 API。
更新 1
应用程序正在保存TokenValidationParameters 并在应用程序控制器中使用bootstrapContext.Token 来获取服务器到服务器的通信。
根据@dsrockis,我正试图在验证结束后不久从 Web 应用程序中获取id_token(不在应用程序控制器内)。
我在Startup 类中的OpenIdConnectAuthenticationOptions.Notifications 中使用SecurityTokenValidated 调用程序。 SecurityTokenValidated 接收到 SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> 类型的参数,但我不确定在哪里可以找到 id_token。方法如下。
private OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(string policy)
{
return new OpenIdConnectAuthenticationOptions
{
// For each policy, give OWIN the policy-specific metadata address, and
// set the authentication type to the id of the policy
MetadataAddress = String.Format(aadInstance, tenant, policy),
AuthenticationType = policy,
// These are standard OpenID Connect parameters, with values pulled from web.config
ClientId = clientId,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
//NEW METHOD INVOKE ************************************
//******************************************************
SecurityTokenValidated = OnSecurityTokenValidated
//******************************************************
},
Scope = "openid",
ResponseType = "id_token",
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
SaveSigninToken = true
},
};
}
//NEW METHOD ************************************
private Task OnSecurityTokenValidated(
SecurityTokenValidatedNotification<OpenIdConnectMessage,
OpenIdConnectAuthenticationOptions> arg)
{
//QUESTION ********************************************************
//How to find the just saved id_token using incoming parameter, arg
//*****************************************************************
return Task.FromResult(0);
}
更新 2
我尝试了AuthorizationCodeReceived 而不是SecurityTokenValidated,但它根本没有被调用。正如这里所讨论的,我的redirect url does have an ending slash 也是如此。
有什么想法吗?
【问题讨论】:
标签: authentication asp.net-mvc-5 asp.net-web-api2 azure-ad-b2c http-token-authentication