【发布时间】:2020-08-04 01:30:35
【问题描述】:
我有一个名为 MyApi 的 Api,我使用另一个带有 Identityserver4 的 asp.net 核心应用程序来保护 MyApi,现在我的 MyApi 没有任何问题,但是,我想保存用户的 NationalCode,所以我应该将此保存在我的 IdentityServer 数据库中,但无法在我的 IdentityServer 项目中获取 UserId(使用 User.Identity.Name),我在之前的问题中遇到了同样的问题
User.Identity.Name is null in my ASP.NET Core Web API
现在我的 IdentityServer4 项目中遇到了这个问题,所以
我可以使用 MyApi 令牌还是应该获取一个新令牌来向我的 idenittyserver4 项目发送请求?
如果我可以 MyAPI token ,我应该如何添加配置来解决问题?
如果我应该为我的 IdentityServer4 项目获取新令牌,我需要让用户再次登录吗?!!!
编辑
我在下面的链接中找到了一个教程,但我的问题还没有解决。
http://docs.identityserver.io/en/latest/topics/add_apis.html
我用下面的方法播种了我的 IdentityDatabase
public async Task AddIdenityServerApiToResources(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var ccontext = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
ccontext.Database.Migrate();
//=============================================================
ccontext.ApiResources.Add(new ApiResource(IdentityServerConstants.LocalApi.ScopeName) {
UserClaims =
{
JwtClaimTypes.Name,
JwtClaimTypes.Subject,
JwtClaimTypes.Role,
}
}.ToEntity());
//Add ApiResource To Client's Scope
var Clients = ccontext.Clients.Include(e => e.AllowedScopes);
foreach (var item in Clients)
{
item.AllowedScopes.Add(new IdentityServer4.EntityFramework.Entities.ClientScope() { Scope = IdentityServerConstants.LocalApi.ScopeName });
}
var Count = await ccontext.SaveChangesAsync();
if (Count > 0)
{
}
}
}
【问题讨论】:
标签: authentication asp.net-core asp.net-web-api2 asp.net-identity identityserver4