2016-09-06 62 views
8

我遇到了ASP.NET Core中的身份验证管道的一些问题。我的方案是我想向已经使用OpenID Connect和Azure AD进行身份验证的用户发出挑战。有多种场景可供您使用,例如在AAD v2端点场景中请求其他范围时。在ASP.NET核心中重新验证经过身份验证的用户

这很像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); 
    } 

和动作看起来是这样的:

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

您是否考虑使用AuthenticationSchemes?如果您想忽略某些操作的cookieauthentication,则可以使用另一种方案(如azureaadscheme)的Authorize特性。 –

+0

不幸的是,这也没有帮助。我可以设置所有我想要的方案并为挑战指定一个方案,但Cookie身份验证仍然以某种方式涉及处理它。请注意,我没有使用Authorize属性。用户已经通过身份验证,并且我手动发起挑战。 – VolatileCoder

+0

你可以发布startup.cs和操作方法吗? –

回答

0
Try to sign out: 

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

这也行不通。工作是发布SignOut并重定向到另一个行动,然后发起挑战。但显然这意味着另一次往返,因此并不理想。签名后立即发出挑战会覆盖签名可能创建的任何响应。 – VolatileCoder

+0

我不确定它是否解决了您的问题,但我添加了远程注销功能,请参阅最新答案。 –

+0

不幸的是,这并没有什么区别。在没有重定向的情况下,最后一条语句规定了响应的样子,所以这与发出没有任何退出内容的挑战相同。而这本身(最后一个陈述决定了反应)是好的;只是这个挑战不会产生像我期望的那样基于ASP.NET vPrev体验的响应。 – VolatileCoder

2

我发现了一个提示和解决方案这里: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能够调用AuthauticationManager.ChallengeAsync与ChallengeBehavior.Unauthorized行为。

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

我测试了它,它工作得非常好(我的目标仅仅是扩展每个用户的Google范围)。

相关问题