C# 如何在同一应用程序中验证/授权MVC web app和web api

C# 如何在同一应用程序中验证/授权MVC web app和web api,c#,asp.net,asp.net-mvc,asp.net-web-api,azure-active-directory,C#,Asp.net,Asp.net Mvc,Asp.net Web Api,Azure Active Directory,我们使用asp.net MVC框架和Azure active directory开发了一个用于身份验证/授权的web应用程序。 现在的问题是,我们将在该webapp中使用api。为了验证web api,我们可以使用相同的请求令牌,这与我们成功授权webapp web应用时获得的请求令牌相同 谢谢, Tamilselvan S.您可以为支持OWIN的web应用程序添加多个身份验证中间件。要同时支持cookie和bear身份验证,您可以参考以下代码: app.UseWindowsAzureActiv

我们使用asp.net MVC框架和Azure active directory开发了一个用于身份验证/授权的web应用程序。 现在的问题是,我们将在该webapp中使用api。为了验证web api,我们可以使用相同的请求令牌,这与我们成功授权webapp web应用时获得的请求令牌相同

谢谢,
Tamilselvan S.

您可以为支持OWIN的web应用程序添加多个身份验证中间件。要同时支持cookie和bear身份验证,您可以参考以下代码:

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,
        }

    });

问题解决了吗?如果没有,请随时让我知道你被阻止的步骤。