加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 重新挑战ASP.NET Core中经过身份验证的用户

发布时间:2020-12-16 04:30:30 所属栏目:asp.Net 来源:网络整理
导读:我在ASP.NET Core中遇到了一些身份验证管道问题.我的方案是我想向已经使用OpenID Connect和Azure AD进行身份验证的用户发出挑战.您可以在多种情况下执行此操作,例如在AAD v2端点方案中请求其他范围时. 这就像ASP.NET MVC中的魅力一样,但在ASP.NET Core MVC中
我在ASP.NET Core中遇到了一些身份验证管道问题.我的方案是我想向已经使用OpenID Connect和Azure AD进行身份验证的用户发出挑战.您可以在多种情况下执行此操作,例如在AAD v2端点方案中请求其他范围时.

这就像ASP.NET MVC中的魅力一样,但在ASP.NET Core MVC中,用户被重定向到cookie身份验证中间件中配置的Access Denied页面. (当用户未登录时,发出挑战按预期工作.)

在网上搜索并为我的中间件选项尝试不同的参数几个小时之后,我开始怀疑我要么缺少明显的东西,要么这种行为是设计的,我需要以其他方式解决我的要求.有人对此有任何想法吗?

编辑:我的Startup.cs的相关部分如下所示:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddAuthentication(
            SharedOptions => SharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
    }

    public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerFactory)
    {
        // <snip...>

        app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme });

        var options = new OpenIdConnectOptions
        {
            AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme,ClientId = ClientId,Authority = Authority,CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],ResponseType = OpenIdConnectResponseType.CodeIdToken,PostLogoutRedirectUri = "https://localhost:44374/",TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            {
                ValidateIssuer = false
            }
        };
        options.Scope.Add("email");
        options.Scope.Add("offline_access");

        app.USEOpenIdConnectAuthentication(options);
    }

Action看起来像这样:

public void RefreshSession()
    {
        HttpContext.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme,new AuthenticationProperties { RedirectUri = "/" });
    }

解决方法

我在这里找到了一个提示和解决方案: https://github.com/aspnet/Security/issues/912.
ChallengeBehavior.Unauthorized是“关键”.

这篇文章给出了当前(2016年11月 – ASPNet 1.0.1)的解决方法:https://joonasw.net/view/azure-ad-b2c-with-aspnet-core

您需要一个新的ActionResult才能使用ChallengeBehavior.Unauthorized行为调用AuthauticationManager.ChallengeAsync.

一旦问题https://github.com/aspnet/Mvc/issues/5187将成功关闭,这应该被整合.

我测试了它并且它运行得非常好(我的目标只是基于每个用户扩展Google范围).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读