0

我们已经开发了一个使用asp.net MVC框架和Azure活动目录进行身份验证/授权的Web应用程序。 现在的问题是我们要在该webapp中使用api。为了验证web api,我们可以使用与我们成功授权webapp web应用程序时相同的请求令牌。如何在同一应用程序中验证/授权MVC Web应用程序和Web API

感谢, Tamilselvan S.

+0

问题是否已解决?如果没有,请随时让我知道你一步被阻止。 –

回答

1

您可以添加对Web应用程序的多个认证中间件支持OWIN。要同时支持cookie和认证,请参考以下代码:

app.UseWindowsAzureActiveDirectoryBearerAuthentication(
    new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
    { 
     Audience = ConfigurationManager.AppSettings["ida:Audience"], 
     Tenant = ConfigurationManager.AppSettings["ida:Tenant"], 
}); 

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions 
    { 

     ClientId = clientId, 
     Authority = Authority, 
     Notifications = new OpenIdConnectAuthenticationNotifications() 
     { 
      RedirectToIdentityProvider = (context) => 
      { 
       // This ensures that the address used for sign in and sign out is picked up dynamically from the request 
       // this allows you to deploy your app (to Azure Web Sites, for example)without having to change settings 
       // Remember that the base URL of the address used here must be provisioned in Azure AD beforehand. 
       string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase; 
       context.ProtocolMessage.RedirectUri = appBaseUrl + "/"; 
       context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl; 

       return Task.FromResult(0); 
      }, 

      AuthenticationFailed = (context) => 
      { 
       // Suppress the exception if you don't want to see the error 
       context.HandleResponse(); 

       return Task.FromResult(0); 
      } 

     }, 
     TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters 
     { 
      ValidateIssuer = false, 
     } 

    }); 
相关问题