【问题标题】:How Use Authentication and Authoriziation of my IdentityServer with itself?如何使用我的 IdentityServer 自身的身份验证和授权?
【发布时间】: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


    【解决方案1】:

    在 IdentityServer4 startup.cs 中配置服务

    您应该像对待任何其他需要由 idserver4 保护的 api 一样对待 api。

    含义:使用 AddAuthentication 和 AddJWTToken:

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.Authority = "https:// idserver4 ";
                options.RequireHttpsMetadata = true;
                options.Audience = "api name";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name",
                    RoleClaimType = "role"
                };
            });
    

    在 API 控制器中:

    使用 Authorize Attirbute 并像这样确定身份验证方案:

    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    

    【讨论】:

    【解决方案2】:

    我通过以下链接解决了问题:

    http://docs.identityserver.io/en/latest/topics/add_apis.html

    但问题是我没有在我的控制器上使用 AuthorizeLocalApi.PolicyName 策略

    [Route("localApi")]
    [Authorize(LocalApi.PolicyName)]
    public class LocalApiController : ControllerBase
    {
        public IActionResult Get()
        {
            // omitted
        }
    }
    

    问题解决后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      相关资源
      最近更新 更多