mTr*_*lby 4 c# jwt bearer-token asp.net-core identityserver4
我试图搜索该问题的解决方案,但没有找到正确的搜索文本。
我的问题是,如何配置我的IdentityServer,使其也可以接受/授权带有BearerTokens的Api请求?
我已配置并正在运行IdentityServer4。我还在我的IdentityServer上配置了测试API,如下所示:
[Authorize]
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
Run Code Online (Sandbox Code Playgroud)
在我的startup.cs中,ConfigureServices()如下:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
// configure identity server with stores, keys, clients and scopes
services.AddIdentityServer()
.AddCertificateFromStore(Configuration.GetSection("AuthorizationSettings"), loggerFactory.CreateLogger("Startup.ConfigureServices.AddCertificateFromStore"))
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.DefaultSchema = "auth";
options.ConfigureDbContext = builder =>
{
builder.UseSqlServer(databaseSettings.MsSqlConnString,
sql => sql.MigrationsAssembly(migrationsAssembly));
};
})
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.DefaultSchema = "auth";
options.ConfigureDbContext = builder =>
builder.UseSqlServer(databaseSettings.MsSqlConnString,
sql => sql.MigrationsAssembly(migrationsAssembly));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
// this uses Asp Net Identity for user stores
.AddAspNetIdentity<ApplicationUser>()
.AddProfileService<AppProfileService>()
;
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = authSettings.AuthorityUrl;
options.RequireHttpsMetadata = authSettings.RequireHttpsMetadata;
options.ApiName = authSettings.ResourceName;
})
Run Code Online (Sandbox Code Playgroud)
和Configure()如下:
// NOTE: 'UseAuthentication' is not needed, since 'UseIdentityServer' adds the authentication middleware
// app.UseAuthentication();
app.UseIdentityServer();
Run Code Online (Sandbox Code Playgroud)
我有一个配置为允许隐式授予类型的客户端,并且将配置的ApiName包括为AllowedScopes之一:
new Client
{
ClientId = "47DBAA4D-FADD-4FAD-AC76-B2267ECB7850",
ClientName = "MyTest.Web",
AllowedGrantTypes = GrantTypes.Implicit,
RequireConsent = false,
RedirectUris = { "http://localhost:6200/assets/oidc-login-redirect.html", "http://localhost:6200/assets/silent-redirect.html" },
PostLogoutRedirectUris = { "http://localhost:6200/?postLogout=true" },
AllowedCorsOrigins = { "http://localhost:6200" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"dev.api",
"dev.auth" // <- ApiName for IdentityServer authorization
},
AllowAccessTokensViaBrowser = true,
AllowOfflineAccess = true,
AccessTokenLifetime = 18000,
},
Run Code Online (Sandbox Code Playgroud)
当我使用Postman访问受保护的API时,即使将有效的Bearer Token添加到了Request标头中,它总是重定向到Login页面。
注释掉[Authorize]属性将正确返回响应,但是User.Claims当然是空的。
当登录到IdentityServer(通过浏览器)然后访问API(通过浏览器)时,它还将返回响应。这次,User.Claims可用。
有一个示例在IdentityServer内部共同托管受保护的API:IdentityServerAndApi
我将他们的创业公司与您的创业公司进行快速比较,是他们打电话AddJwtBearer
而不是AddIdentityServerAuthentication
:
services.AddAuthentication()
.AddJwtBearer(jwt => {
jwt.Authority = "http://localhost:5000";
jwt.RequireHttpsMetadata = false;
jwt.Audience = "api1";
});
Run Code Online (Sandbox Code Playgroud)
该Authorize
属性还设置身份验证方案:
[Authorize(AuthenticationSchemes = "Bearer")]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3110 次 |
最近记录: |