1

我看到IdentityServer3和外部提供程序有奇怪的行爲。我希望有人能夠指出我錯過的東西。IdentityServer3,Azure Active Directory外部提供程序,消息='Action returned'System.Web.Http.Results.Unauthorized''

摘要

第一外部登錄請求設置瀏覽器等待只能通過記錄退還,結果在下面看到的錯誤請求。如果我在瀏覽器中取消請求並立即再次點擊該按鈕,它將按預期工作,瀏覽器將被髮送到外部登錄屏幕。


配置

我根據一些參考和文檔配置IDSrv3以及我能確定,利用Azure的Active Directory中。

var wsFedOptions = new WsFederationPluginOptions(options); 
    wsFedOptions.Factory.Register(new Registration<IEnumerable<RelyingParty>>(RelyingParties.Get())); 
    wsFedOptions.Factory.RelyingPartyService = new Registration<IRelyingPartyService>(typeof(InMemoryRelyingPartyService)); 
    app.UseWsFederationPlugin(wsFedOptions); 



var aad = new OpenIdConnectAuthenticationOptions 
      { 
       AuthenticationType = "AzureAd", 
       Caption = "Azure AD", 
       SignInAsAuthenticationType = signInAsType, 
       PostLogoutRedirectUri = Settings.LogoutRedirect, 
       Authority = Settings.AADAuthority, 
       ClientId = Settings.AADClientId, 
       RedirectUri = Settings.AADRedirectUrl 
      }; 

      app.UseOpenIdConnectAuthentication(aad); 

在登錄視圖中,我如上所述(Azure AD)提供了外部登錄按鈕。第一次點擊這個按鈕,瀏覽器只是等待主機...

在日誌中,我發現了以下錯誤。


iisexpress.exe Information: 0 : 2017-04-05 08:28:09.708 -05:00 [Information] External login requested for provider: "AzureAd" 
iisexpress.exe Information: 0 : 2017-04-05 08:28:09.714 -05:00 [Information] Triggering challenge for external identity provider 
LibLog Information: 0 : [2017-04-05T13:28:09.7176576Z] Level=Info, Kind=End, Category='System.Web.Http.Action', Id=800000ad-0002-fb00-b63f-84710c7967bb, Message='Action returned 'System.Web.Http.Results.UnauthorizedResult'', Operation=ReflectedHttpActionDescriptor.ExecuteAsync 
LibLog Information: 0 : [2017-04-05T13:28:09.7206611Z] Level=Info, Kind=End, Category='System.Web.Http.Action', Id=800000ad-0002-fb00-b63f-84710c7967bb, Operation=ApiControllerActionInvoker.InvokeActionAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7216630Z] Level=Info, Kind=Begin, Category='System.Web.Http.Filters', Id=800000ad-0002-fb00-b63f-84710c7967bb, Message='Action filter for 'LoginExternal(String signin, String provider)'', Operation=NoCacheAttribute.OnActionExecutedAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7226640Z] Level=Info, Kind=End, Category='System.Web.Http.Filters', Id=800000ad-0002-fb00-b63f-84710c7967bb, Operation=NoCacheAttribute.OnActionExecutedAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7226640Z] Level=Info, Kind=Begin, Category='System.Web.Http.Filters', Id=800000ad-0002-fb00-b63f-84710c7967bb, Message='Action filter for 'LoginExternal(String signin, String provider)'', Operation=SecurityHeadersAttribute.OnActionExecutedAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7236655Z] Level=Info, Kind=End, Category='System.Web.Http.Filters', Id=800000ad-0002-fb00-b63f-84710c7967bb, Operation=SecurityHeadersAttribute.OnActionExecutedAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7246669Z] Level=Info, Kind=End, Category='System.Web.Http.Controllers', Id=800000ad-0002-fb00-b63f-84710c7967bb, Operation=AuthenticationController.ExecuteAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7251836Z] Level=Info, Kind=End, Category='System.Web.Http.MessageHandlers', Id=800000ad-0002-fb00-b63f-84710c7967bb, Operation=PassiveAuthenticationMessageHandler.SendAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7261856Z] Level=Info, Kind=End, Category='System.Web.Http.MessageHandlers', Id=800000ad-0002-fb00-b63f-84710c7967bb, Operation=DependencyScopeHandler.SendAsync, Status=401 (Unauthorized) 
LibLog Information: 0 : [2017-04-05T13:28:09.7271879Z] Sending response, Status=401 (Unauthorized), Method=GET, Url=https://localhost:44396/identity/external?provider=AzureAd&signin=2d92dd18a6106c9b029eb8742d4117a1, Id=800000ad-0002-fb00-b63f-84710c7967bb, Message='Content-type='none', content-length=unknown' 

瀏覽器將無限期地繼續在本地主機上等待。 如果我停止請求並立即再次點擊按鈕,一切都按預期工作。

+0

錯誤似乎在IDSrv3 AuthenticatioinController.cs在行330前夕context.Authentication發生的OpenIdAuthenticationOptions .Challenget(authProp,provider)那麼這個方法立即返回Unauthorized(); –

回答

0

原來這個問題與Katana OIDC MW中的死鎖問題有關。 解決方法是創建自定義IConfigurationManager並在啓動時手動獲取元數據。與Thinktecture提出的類似。

https://github.com/IdentityServer/IdentityServer3/blob/master/source/Host.Configuration/Extensions/SyncConfigurationManager.cs

代OpenIdConnectConfiguration



var manager = new SyncConfigurationManager(new ConfigurationManager < OpenIdConnectConfiguration > (Settings.AADAuthority + "/.well-known/openid-configuration")); 

那麼管理者添加到



    var aad = new OpenIdConnectAuthenticationOptions 
       { 
        AuthenticationType = "AzureAd", 
        Caption = "Marquis Azure AD", 
        SignInAsAuthenticationType = signInAsType, 
        PostLogoutRedirectUri = Settings.LogoutRedirect, 
        Authority = Settings.AADAuthority, 
        ClientId = Settings.AADClientId, 
        RedirectUri = Settings.AADRedirectUrl, 
        ConfigurationManager = manager 
       }; 

1

基於OpenIdConnectAuthenticationOptions代碼似乎是正確的。我也是用下面的代碼登錄的IdentityServer3與Azure的AD帳戶,它很適合我:

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     Log.Logger = new LoggerConfiguration() 
      .MinimumLevel.Debug() 
      .WriteTo.Trace() 
      .CreateLogger(); 

     var users = new List<InMemoryUser>() 
     { 
      new InMemoryUser 
      { 
       Username="Jack", Password="Jack", 
       Claims= new List<Claim> 
       { 
        new Claim("name","Jack"), 
        new Claim("email","[email protected]"), 
        new Claim("role","Admin"), 
       } 
      } 
     }; 

     var clients = new Client[] 
     { 
      new Client 
      { 
       ClientId="mvc", 
       ClientName="MVC Demo Client", 
       Flow=Flows.Implicit, 
       RedirectUris=new List<string> 
       { 
        "http://localhost:9000", 
        "http://localhost:1409/" 
       }, 
       AllowedScopes=new List<string> 
       { 
        "openid","email","profile","roles" 
       } 
      } 
     }; 

     var scopes = new Scope[] 
      { 
       StandardScopes.OpenId, 
       StandardScopes.ProfileAlwaysInclude, 
       StandardScopes.EmailAlwaysInclude, 
       new Scope 
       { 
        Name="roles", 
        Claims=new List<ScopeClaim> 
        { 
         new ScopeClaim("role") 
        }, 
        Type=ScopeType.Identity 
       } 
      }; 

     var factory = new IdentityServerServiceFactory(); 
     factory.UseInMemoryClients(clients); 
     factory.UseInMemoryScopes(scopes); 
     factory.UseInMemoryUsers(users); 

     var cert = LoadCertificate(); 

     app.UseIdentityServer(new IdentityServerOptions 
     { 
      SiteName = "NDC Demo", 
      SigningCertificate = cert, 
      Factory = factory, 
      AuthenticationOptions = new AuthenticationOptions 
      { 
       IdentityProviders = ConfigureAdditionalIdentityProviders, 
       EnableAutoCallbackForFederatedSignout = true 
      } 
     }); 
    } 

    public static void ConfigureAdditionalIdentityProviders(IAppBuilder app, string signInAsType) 
    { 
     app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
     { 
      AuthenticationType = "aad", 
      Caption = "Azure AD", 
      SignInAsAuthenticationType = signInAsType, 

      Authority = "https://login.microsoftonline.com/04e14a2c-0e9b-42f8-8b22-3c4a2f1d8800", 
      ClientId = "eca61fd9-f491-4f03-a622-90837bbc1711", 
      RedirectUri = "https://localhost:44333/core/aadcb", 
     }); 
    } 

    static X509Certificate2 LoadCertificate() 
    { 
     var baseFolder = AppDomain.CurrentDomain.BaseDirectory; 
     string certificatePath = $"{baseFolder}\\Certificates\\mycompanyname.pfx"; 
     return new X509Certificate2(certificatePath, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); 
    } 
} 

然後我們就可以用IdentityServer3互動與下面的要求:

https://localhost:44333/connect/authorize?response_type=id_token&client_id=mvc&redirect_uri=http://localhost:9000&scope=openid+email+profile+roles&nonce=123

請讓我看看是否有幫助。

+0

您的配置中沒有任何內容不在我的配置中,除了我使用login.windows.net/[tenantID]。就像我說的那樣,它可以工作,但只有當我取消第一個請求時纔有效。我懷疑IDSrv3是這個問題的根源,謝謝你的努力。 –