简体   繁体   English

如何在同一应用程序中对MVC Web应用程序和Web API进行身份验证/授权

[英]How to authenticate/Authorize the MVC web app and web api in the same application

We have developed a web application using asp.net MVC framework with Azure active directory for Authentication/Authorization. 我们使用asp.net MVC框架开发了一个Web应用程序,该框架具有用于身份验证/授权的Azure活动目录。 Now the question is we are going to use api in that webapp. 现在的问题是,我们将在该Web应用程序中使用api。 For authenticating web api can we use same the request token which we get when we the authorize successfully for the webapp web app. 为了对Web api进行身份验证,我们可以使用与成功授权webapp Web应用程序时获得的请求令牌相同的令牌。

Thanks, Tamilselvan S. 谢谢,Tamilselvan S.

You can add multiple authentication middle ware for the web app supports OWIN. 您可以为Web应用程序支持OWIN添加多个身份验证中间件。 To support both cookie and bear authentication, you can refer the code below: 要同时支持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,
        }

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户从MVC应用程序持久化和身份验证到Web API - How to persist and authenticate users from an MVC application to a Web API 如何使用客户端证书在Web API中进行身份验证和授权 - How to use a client certificate to authenticate and authorize in a Web API 如何在成为旧版ASP.NET MVC应用程序的一部分时对Web API 2中的用户进行身份验证? - How to authenticate user in Web API 2 when being part of a legacy ASP.NET MVC application? MVC Web应用程序和API Web应用程序可以在同一池中运行吗? - Can an MVC web app and an API web app run in the same pool? 如何使用Web API JWT令牌对MVC用户登录表单进行身份验证? - How to authenticate MVC user login form with web API JWT token? 在MVC Web应用程序中使用URL令牌对用户进行身份验证 - Authenticate user with a URL token in an MVC web application 如何在 WPF 应用程序中使用 Z3A580F142203677F0BC30898 AZFZ 验证 web api - How to authenticate web api in WPF app using Azure AD 如何使用应用程序ID和应用程序密钥授权/身份验证API请求 - How to Authorize/Authenticate an API request using an app id and app key 使用来自Web API的访问令牌在Web应用上授权 - Authorize at web app with access token from web api 验证站点/应用程序以访问Web API服务 - Authenticate a site/app to access a Web API Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM