2016-09-06 63 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範圍)。

相關問題