2017-03-09 143 views
0

我有一個ASP.NET MVC應用程序託管IdentityServer3,但我想在同一主機上託管Angular + WebAPI 2自定義管理應用程序。該管理員應用程序正在使用oidc-client庫進行身份驗證。下面是我的Startup類,用於配置IdentityServer並調用UseIdentityServerBearerTokenAuthentication方法。正如您所看到的,我在異步任務中調用了該方法,因爲在IdentityServer啓動之前很快就會發生這種情況。身份服務器身份驗證管理應用程序在同一主機

身份驗證的作品,我的Angular Ajax請求充滿了有效的訪問令牌,但我沒有得到WebApi控制器上的任何索賠。 ClaimsPrincipal具有空的聲明列表,而IsAuthenticated是false。 另外我的客戶端配置已正確設置。這個設置有什麼問題嗎?

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


     var factory = new IdentityServerServiceFactory(); 

     factory.Register<IdentityDatabaseModel>(new Registration<IdentityDatabaseModel>(typeof(IdentityDatabaseModel))); 
     factory.Register<UserDataService>(new Registration<UserDataService>(typeof(UserDataService))); 
     factory.Register<TokenDataService>(new Registration<TokenDataService>(typeof(TokenDataService))); 
     factory.Register<ClaimsDataService>(new Registration<ClaimsDataService>(typeof(ClaimsDataService))); 
     factory.Register<ClientDataService>(new Registration<ClientDataService>(typeof(ClientDataService))); 


     factory.UserService = new Registration<IUserService>(typeof(UserService)); 

     factory.RefreshTokenStore = new Registration<IRefreshTokenStore, RefreshTokenStore>(); 
     factory.ClientStore = new Registration<IClientStore, ClientStore>(); 


     factory.UseInMemoryScopes(WebApplication1.Models.IS.Scopes.Get()); 

     var options = new IdentityServerOptions 
     { 
      SigningCertificate = Certificate.Get(), 
      Factory = factory, 
      RequireSsl = false, 
      LoggingOptions = new LoggingOptions 
      { 
       //EnableHttpLogging = true, 
       EnableKatanaLogging = true, 
       EnableWebApiDiagnostics = true, 
       WebApiDiagnosticsIsVerbose = true 
      }, 
      EnableWelcomePage = false 
     }; 
     app.UseIdentityServer(options); 

     #region IdentityServer authentication 
     JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); 

     Task.Factory.StartNew(() => { 
      System.Threading.Thread.Sleep(5000); 
      app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
      { 
       Authority = "http://localhost:17343", 
       RequiredScopes = new[] { "openid", "email", "roles", "profile" }, 
       ClientId = "lsidentity", 
       ClientSecret = "secret" 
      }); 
     }); 
     #endregion 
    } 
} 
+0

我認爲你在你的web api中使用了錯誤的範圍。您的web api應該定義自己的作用域,在這個作用域中需要定義訪問令牌中的哪些聲明,然後將它們推入並在您的web api控制器中可用 – Jinish

回答

0

的問題是,我需要配置發行者名稱和SigningCertificate中的WebAPI的配置,所以它看起來是這樣的:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
     { 
      Authority = "http://localhost:17343", 
      //Authority = "http://192.168.254.3:303", 
      RequiredScopes = new[] { "openid", 
       "email", "profile" }, 
      IssuerName = "http://localhost:17343", //added this 
      SigningCertificate = Certificate.Get(), // ...and this 
      // client credentials for the introspection endpoint 
      ClientId = "lsidentity", 
      ClientSecret = "secret".Sha256() 
     }); 

有GitHub上的問題,但我沒有先找到它。 https://github.com/IdentityServer/IdentityServer3.AccessTokenValidation/issues/38

也沒有必要將此稱爲任務,它現在工作正常。