身份服务器身份验证管理应用程序在同一主机
问题描述:
我有一个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
}
}
答
的问题是,我需要配置发行者名称和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
也没有必要将此称为任务,它现在工作正常。
我认为你在你的web api中使用了错误的范围。您的web api应该定义自己的作用域,在这个作用域中需要定义访问令牌中的哪些声明,然后将它们推入并在您的web api控制器中可用 – Jinish