带有 OpenIdAuthentication 的 ASP.NET:如果未授权,则重定向到 url

ASP.NET with OpenIdAuthentication: redirect to url if not authorized(带有 OpenIdAuthentication 的 ASP.NET:如果未授权,则重定向到 url)
本文介绍了带有 OpenIdAuthentication 的 ASP.NET:如果未授权,则重定向到 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试编写一个使用混合身份验证方案的 ASP.NET 应用程序.用户可以将其用户名和密码哈希存储在 UserStore 中,也可以通过 Azure Active Directory 进行身份验证.

I am attempting to write an ASP.NET application that uses a hybrid authentication scheme. A user can either have his username and password hash stored in the UserStore, or he can authenticate via Azure Active Directory.

我已经创建了如图所示的登录表单.它具有标准的 UserNamePassword 输入,还具有通过 Active Directory 登录"按钮.

I have created the login form pictured. It has the standard UserName and Password inputs, but also has a "Login via Active Directory" button.

这很好用.

现在解决问题:应用程序的主页具有 [Authorize] 属性.

Now for the problem: The application's home page has the [Authorize] attribute.

public class DefaultController : Controller
{
    [Authorize]
    public ViewResult Index()
    { 
    // Implementation
    }
}

如果用户没有登录,我希望它重定向到页面Account/Login,让用户选择认证方式.

If the user is not logged in, I want it to redirect to the page Account/Login, allowing the user to choose the authentication method.

IAppBuilder.UseOpenIdConnectAuthentication 添加到管道设置后,它不再重定向到该页面.相反,它直接进入 Microsoft 登录页面.

Once I added IAppBuilder.UseOpenIdConnectAuthentication to the pipeline setup, it no longer redirects to that page. Instead, it goes straight to the Microsoft Login page.

我如何配置它以使 OpenID 身份验证成为系统的一部分,但允许我指定在用户未通过身份验证时如何执行重定向?

How do I configure it so that OpenID authentication is part of the system, but allow me to specify how to perform redirections when the user is not authenticated?

这是我设置管道的代码:

Here's the code where I set up the pipeline:

appBuilder.SetDefaultSignInAsAuthticationType(CookieAuthenticationDefaults.AuthenticationType_;
var cookieAuthenticationOptions = new CookieAuthenticationOptions
{
     AuthenticationType = DefaultAuthenticationType.ApplicationCookie,
     LoginPath = new Microsoft.Owin.PathString("/Account/Login"),
     Provider = new Security.CookieAuthenticationProvider()
};
appBuilder.UseCookieAuthentication(cookieAuthenticationOptions);
// Now the OpenId authentication
var notificationHandlers = new OpenIdConnectAuthenticationNotificationHandlers 
{
   AuthorizationCodeReceived = async(context) => {
       var jwtSecurityToken = context.JwtSecurityToken;
       // I've written a static method to convert the claims
       // to a user
       var user = await GetOrCreateUser(context.OwinContext, jwtSecurityToken.Claims);
       var signInManager = context.OwinContext.Get<SignInManager>();
       await signInManager.SignInAsync(user, true, false);
   }
}
var openIdOptions = new OpenIdConnectAuthenticationOptions
{
     ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
     Authority = "https://login.microsoftonline.com/xxxxx.onmicrosoft.com",
     PostLogoutRedirectUri = "https://localhost:52538/Account/Login",
     Notifications = notifcationHandlers
}
appBuilder.UseOpenIdConnectAuthentication(openIdOptions);

当您单击Active Directory 登录"时,它会发布到Account/SignInWithOpenId"

When you click "Active Directory Signin", it posts to "Account/SignInWithOpenId"

public ActionResult SignInWithOpenId()
{
    // Send an OpenID Connect sign-in request.
    if (!Request.IsAuthenticated)
    {
        var authenticationProperties = new AuthenticationProperties
        {
            RedirectUri = "/"
        };
        HttpContext.GetOwinContext().Authentication.Challenge
        (
            authenticationProperties,
            OpenIdConnectAuthenticationDefaults.AuthenticationType
        );
        return new EmptyResult();
    }
    else
    {
        return RedirectToAction("Index", "Default");
    }
}

推荐答案

IAppBuilder.UseOpenIdConnectAuthentication(...) 的调用将 Owin 中间件组件放入管道中.当 ASP.NET MVC 返回 401(未授权)的 HttpResponse 时,Owin 中间件组件会检测到这一点并将其更改为 Http Redirect(代码 302),并且重定向路径是 Open Id 提供程序.

The call to IAppBuilder.UseOpenIdConnectAuthentication(...) puts an Owin middleware component in the pipeline. When ASP.NET MVC returns an HttpResponse of 401 (Unauthorized), the Owin Middleware component detects this and changes it to an Http Redirect (code 302), and the redirection path is to the Open Id provider.

但是有一种方法可以解决这个问题:在中间件组件执行重定向之前,它会调用回调 RedirectToIdentityProvider.从这里,您可以覆盖此重定向.

But there's a way to get around this: before the middleware component performs the redirect, it invokes the callback RedirectToIdentityProvider. From here, you can override this redirection.

这是我的代码,它会覆盖重定向,除非它来自请求路径 Account/SignInWithOpenId.

Here is my code that overrides the redirection unless it is from the request path Account/SignInWithOpenId.

var notificationHandlers = new OpenIdConnectAuthenticationNotifications
{
    AuthorizationCodeReceived = async(context) => {
       // Sign in the user here
    },
    RedirectToIdentityProvider = (context) => {
        if(context.OwinContext.Request.Path.Value != "/Account/SignInWithOpenId")
        {
             context.OwinContext.Response.Redirect("/Account/Login");
             context.HandleResponse();
        }
        return Task.FromResult(0);
    }
}
var openIdOptions = new OpenIdConnectAuthenticationOptions
{
     ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
     Authority = "https://login.microsoftonline.com/xxxxx.onmicrosoft.com",
     PostLogoutRedirectUri = "https://localhost:52538/Account/Login",
     Notifications = notifcationHandlers
}
appBuilder.UseOpenIdConnectAuthentication(openIdOptions);

这篇关于带有 OpenIdAuthentication 的 ASP.NET:如果未授权,则重定向到 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
The application named HTTPS://test113.onmicrosoft.com/FTP was not found in the tenant named test113.onmicrosoft.com(在名为 test113.onmicrosoft.com 的租户中找不到名为 HTTPS://test113.onmicrosoft.com/FTP 的应用程序) - IT屋-程序员软件开发技术
Using MSAL and Angular why would one need two separate AAD app registrations?(使用 MSAL 和 Angular 为什么需要两个单独的 AAD 应用注册?)
How to configure .net core angular azure AD authentication?(如何配置.net core angular azure AD 身份验证?)
How do I delete an AppRoleAssignment using the Azure Active Directory .NET SDK?(如何使用 Azure Active Directory .NET SDK 删除 AppRoleAssignment?)
Role Count using Graph Api against a tenant(使用 Graph Api 对租户进行角色计数)