c# - 从 3.1.4 范围更新身份服务器 4 4.0.0 后,使用 Mongo DB 的 asp.net core 3 无效

标签 c# asp.net asp.net-core asp.net-identity identityserver4

在发现文档中,未添加范围IdentityPortal.API

{
    "issuer": "https://localhost:5001",
    "scopes_supported": ["profile", "openid", "email", "offline_access"],   
}

但是,配置中允许的范围如下

private static string apiScope = "IdentityPortal.API";
private static ICollection<string> AllowedScopes()
        {
            return new List<string>
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email,
                apiScope
            };
        }

API资源

   public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource(apiScope, "Falcon Api")
                {
                    Scopes = new List<string>{apiScope},
                    UserClaims =
                    {
                        JwtClaimTypes.Profile,
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email,
                    }
                }
            };
        }

我从 React 应用程序发送范围如下

 scope: "profile openid email IdentityPortal.API offline_access",

在身份服务器中,IdentityPortal.API 未添加为受支持的声明。

这里是 customPersistedGrantStore.cs

public class CustomResourceStore : IResourceStore
{
    protected IRepository _dbRepository;

    public CustomResourceStore(IRepository repository)
    {
        _dbRepository = repository;
    }


    public Task<IEnumerable<IdentityResource>> FindIdentityResourcesByScopeNameAsync(IEnumerable<string> scopeNames)
    {
        var list = _dbRepository.Where<IdentityResource>(e => scopeNames.Contains(e.Name));
        return Task.FromResult(list.AsEnumerable());
    }

    public Task<IEnumerable<ApiScope>> FindApiScopesByNameAsync(IEnumerable<string> scopeNames)
    {
        var list = _dbRepository.Where<ApiScope>(a => scopeNames.Contains(a.Name));
        return Task.FromResult(list.AsEnumerable());
    }

    public Task<IEnumerable<ApiResource>> FindApiResourcesByScopeNameAsync(IEnumerable<string> scopeNames)
    {
        var list = _dbRepository.Where<ApiResource>(a => a.Scopes.Any(s => scopeNames.Contains(s)));
        return Task.FromResult(list.AsEnumerable());
    }

    public Task<IEnumerable<ApiResource>> FindApiResourcesByNameAsync(IEnumerable<string> apiResourceNames)
    {
        var list = _dbRepository.Where<ApiResource>(a => apiResourceNames.Contains(a.Name));
        return Task.FromResult(list.AsEnumerable());
    }

    public Task<Resources> GetAllResourcesAsync()
    {
        var result = new Resources(GetAllIdentityResources(), GetAllApiResources(),null);
        return Task.FromResult(result);
    }
    
    private IEnumerable<IdentityResource> GetAllIdentityResources()
    {
        return _dbRepository.All<IdentityResource>();
    }
    private IEnumerable<ApiResource> GetAllApiResources()
    {
        return _dbRepository.All<ApiResource>();
    }
    private IEnumerable<ApiScope> GetAllApiScopes()
    {
        return _dbRepository.All<ApiScope>();
    }
}

身份服务器设置

services.Configure<MongoDbConfigurationOptionsViewModel>(Configuration);
        services.AddIdentityServer()//.AddProfileService<ProfileService>()
            .AddMongoRepository()
            .AddMongoDbForAspIdentity<ApplicationUser, IdentityRole>(Configuration)
            .AddClients()
            .AddInMemoryApiScopes(Config.AllowedScopes())
            .AddIdentityApiResources()
            .AddPersistedGrants()
            .AddDeveloperSigningCredential();
        
        services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                // base-address of your identityserver
                options.Authority = "https://localhost:5001";

                // name of the API resource
                options.ApiName = "IdentityPortal.API";
            });

配置

 public static IEnumerable<ApiScope> AllowedScopes()
        {
            return new List<ApiScope>
            {
                new ApiScope(IdentityServerConstants.StandardScopes.OpenId),
                new ApiScope(IdentityServerConstants.StandardScopes.Profile),
                new ApiScope(IdentityServerConstants.StandardScopes.Email),
                new ApiScope(apiScope)
            };
        }

最佳答案

问题是您刚刚在 IDS4 设置中添加了 api 资源,您还需要更改代码以添加 API 范围。要添加上面的 api 范围,您可以通过 AddInMemoryApiScopes 添加它。代码是这样的:

services.Configure<MongoDbConfigurationOptionsViewModel>(Configuration);
            services.AddIdentityServer()//.AddProfileService<ProfileService>()
                .AddMongoRepository()
                .AddMongoDbForAspIdentity<ApplicationUser, IdentityRole>(Configuration)
                .AddClients()
                .AddInMemoryApiScopes(Config.AllowedScopes)
                .AddIdentityApiResources()
                .AddPersistedGrants()
                .AddDeveloperSigningCredential();
            

更改代码后,重新生成 token 并在 https://jwt.ms/ 上检查它你应该有一个 prop 为 aud = IdentityPortal.API 并且范围为 IdentityPortal.API

当您使用数据库时,您需要先将您的数据库迁移到新版本,这里有一些脚本可以帮助解决这个问题:https://github.com/RockSolidKnowledge/IdentityServer4.Migration.Scripts/tree/CreateScripts 数据库更新后,确保您在 api 资源上有数据,并且 api 资源的范围与所需范围匹配

查看我的博文 https://github.com/nahidf-adventures/IdentityServer4-adventures/tree/ids4-4/src/IdentityServer以获得更详细的解释。 阅读更多官方文档 here

关于c# - 从 3.1.4 范围更新身份服务器 4 4.0.0 后,使用 Mongo DB 的 asp.net core 3 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62639409/

相关文章:

c# - 如何/在何处将业务规则应用于 POCO 对象?

c# - RegisterStartupScript 是否会增加页面大小

c# - 单色触摸 : can't generate WCF proxy

c# - 提交修改viewstate所需的ajax asp表单

c# - 在 Silverlight 中保留用户首选项

c# - 当模型是 List<SomeModel> 时在 View 中验证模型

c# - 与集合不同类型的迭代变量?

c# - 如何在服务器上未安装 MS Office 的情况下在 asp.net 中将 docx 转换为 pdf

c# - 使用 SQL 数据库的依赖项注入(inject)刷新 ASP.NET Core 中的 Azure Active Directory 访问 token

c# - cookie 过期后如何返回 401 状态而不是 302 重定向?