- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
-6ren">
我有受政策保护的 API Controller 。此策略在 Startup.cs 中配置,如
options.AddPolicy("InternalClient", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c =>
(c.Type == "client_id" && c.Value == "installation-logic-client-id"))));
而 Controller 方法是:
[HttpGet("{familyId}/versions/{version}/infos")]
[Authorize(Policy = "InternalClient")]
public IActionResult GetTestInfo(Guid testFamilyId, string version)
{
..............................
}
并测试上述方法,我从 MockIdentityServer 获取 token 。在那里我正在配置客户端
yield return new Client
{
ClientId = "installation-logic-client-id",
ClientSecrets = new[] {new Secret("installation-logic-client-secret".Sha256())},
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = new[] {"installation-logic-scope"},
AllowOfflineAccess = true,
AccessTokenType = AccessTokenType.Jwt,
RefreshTokenUsage = TokenUsage.OneTimeOnly,
RefreshTokenExpiration = TokenExpiration.Sliding,
Claims = new List<Claim>() // I want these claims to be added in the access_token so that they can be verified while making the request.
{
new Claim("client_id", "installation-logic-client-id")
},
AlwaysSendClientClaims = true,
AlwaysIncludeUserClaimsInIdToken = true,
};
我总是能成功获得 token ,但不幸的是,该 token 不包含我正在测试和正在为其设置策略的声明信息。以下是调用..
private async Task<string> GetTokenForInternalClient()
{
var tokenRequest = new ClientCredentialsTokenRequest()
{
Address = await GetTokenEndpoint(),
ClientId = MockConstants.TokenInstallationLogicClientId,
ClientSecret = MockConstants.TokenInstallationLogicClientSecret,
Scope = MockConstants.TokenInstallationLogicScope
};
var tokenResponse = await
_identityServerClient.RequestClientCredentialsTokenAsync(tokenRequest);
if (tokenResponse.IsError) throw new MockIdentityServerException(tokenResponse);
return tokenResponse.AccessToken; // Here I see very short token. Clearly it doesn't contains the claims.
}
目前,我收到“未经授权” 请求。因为由于 claim 不可用,它没有通过政策。任何人都可以告诉我我做错了什么吗?是否有特定的方法来获取所有声明的 access_token
在 Client 对象内的策略级别将 client_id 更改为 id 之后,还将 TokenType 更改为“Jwt”而不是 opf Reference,我得到了以下 Payload。
{
"nbf": 1574191641,
"exp": 1574195241,
"iss": "http://localhost:5000",
"aud": "installation-logic-scope",
"client_id": "installation-logic-client-id",
"scope": [
"installation-logic-scope"
]
}
更新声明(有效载荷主体)
{
"nbf": 1574198669,
"exp": 1574202269,
"iss": "http://localhost:5000",
"aud": "installation-logic-client-id",
"client_id": "installation-logic-client-id",
"scope": [
"installation-logic-scope"
]
}
Startup.cs
private static void ConfigureAuthorization(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("admin", pb => pb.RequireClaim("Role", "admin", "orgadmin"));
options.AddPolicy("InternalClient", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c =>
((c.Type == "Role" && (c.Value == "admin" || c.Value == "orgadmin")) ||
(c.Type == "id" && c.Value == "installation-logic-client-id")))));
});
}
private void ConfigureDbContexts(IServiceCollection services)
{
........................
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
[UsedImplicitly]
public void Configure(IApplicationBuilder app, PackageHandlingContext dbContext)
{
// middlewares: order is important
app.UseRouting();
app.UseCors("AnyOrigin");
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseEndpoints(endpoints => endpoints.MapControllers());
dbContext.Database.EnsureCreated();
}
最佳答案
我想通了整个故事中的所有问题。
首先在memoy ApiResources中构建时,scopes还应该包含“client_id
”。
Scopes = new List<Scope>
{
new Scope(MockConstants.PackageHandlingScope, new[] {"Role", "client_id"})
},
其次,构建测试客户端,只需要使用"id"
作为声明类型,因为client_
已经带有声明类型前缀。
更新后的客户端是
yield return new Client
{
ClientId = MockConstants.TokenInstallationLogicClientId,
ClientSecrets = new[] {new Secret(MockConstants.TokenInstallationLogicClientSecret.Sha256())},
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = new[] {MockConstants.PackageHandlingScope},
AllowOfflineAccess = true,
AccessTokenType = AccessTokenType.Jwt,
RefreshTokenUsage = TokenUsage.OneTimeOnly,
RefreshTokenExpiration = TokenExpiration.Sliding,
Claims = new List<Claim>
{
new Claim("id", "installation-logic-client-id")
}
};
其他都很好。
@Ruard van Elburg 非常感谢您的评论,您指出了构建测试客户端时必需的“id”内容。因为客户声称 alwazs 在通过网络传输到应用程序时会得到前缀。
谢谢
关于c# - 当授予类型为 ClientCredentialsFlow 时,MockIdentityServer 客户端声明不包含在访问 token 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58939442/
如何授予 VSTS (TFVC) 用户从分支创建新分支的权限?我已经授予他对 ParentBranch 的“管理分支”和“合并”权限。 当他尝试从“ParentBranch”创建一个名为“ChildB
我在我的 oracle 过程中使用 dbms_crypto.encrypt 函数来加密密码。我已连接到 oracle 为: connect sqlplus as sysdba 然后授予权限: gran
如何在不连接到每一台计算机的情况下将我的 android 设备的 adb 访问权限授予我的所有计算机并按允许? 最佳答案 通过 adb 连接到设备时,每台计算机都会将代码写入 android 设备,以
我有一个 package A ,它在另一个 package B 中使用了一些变量和过程在 相同的架构 .现在想搬家package A到 新架构 .我应该授予 new schema 哪些权限用于使用 p
我需要获取数百个 MS Access DB 的元数据,因此我需要自动化元数据收集过程。 我想查询描述的内容 here ,但在查询数据库之前,我需要对 MSysObjects 进行读取 Access 表
我有一个 JApplet,我想授予它读写打印等权限。该小程序仅从本地文件系统加载 我已经阅读了java教程的控制小程序部分,并成功创建了一个策略文件,授予权限并指定代码库 http://docs.or
我正在使用 phpmyadmin 来调整用户的权限。 我删除了用户“root”的所有访问权限。现在我看不到表格了。 我尝试用另一个用户登录,但它不允许我。 此时我有什么选择? 第 1 步。 //Sto
Mysql 服务器允许从本地主机访问匿名用户。我也想将此权限扩展到其他机器...即,使用没有任何密码的匿名用户从机器 B 的 mysql 客户端访问在机器 A 中运行的 mysql_server。 我
为所有想要连接的新用户授予 MySQL 数据库访问权限的命令是什么? 考虑这个陈述: CREATE USER 'java'@'localhost' IDENTIFIED BY 'password';
我在尝试通过 t 为 mysql 用户授予数据库权限时遇到了一个问题。我认为这是因为数据库名称中的特殊字符。 这个正在工作: /usr/bin/mysql -uroot -pXz5eaCqwvsT0p
我可以在 GRANT 语句中使用静态数据库名称授予权限。 GRANT SELECT,INSERT ON database_name.table_name TO 'username'@'localhos
授予 Facebook 应用查看我的赞的权限是否允许该应用的所有其他用户也看到我的赞? 或者同一应用的其他用户能否看到我的点赞取决于我的点赞隐私设置? 例如,假设我只允许好友看到我的赞。这是否意味着该
我在这里敲了几个小时的脑袋。 我正在向数据库添加用户和密码,同时尝试授予权限。 GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TA
我正在尝试在 Flask 中创建一个自定义错误页面,我想让错误处理程序访问生成导致错误的 API 调用的请求,以便它返回的错误页面可以更改,具体取决于情况。例如,假设有两个端点: (1) @app.r
我已经编写了一个简短的快速代码来向查找器添加一个按钮,该按钮通过系统 touch 调用在当前目录中创建一个新的空白文件。该扩展可以很好地获取当前目录(通过 FIFinderSyncController
当我运行 hdf namenode -format 时,它想要删除目录 /home/hadoop/hadooptmpdata/dfs/name/current 但它没有权限执行此操作。如何授予它权限?
我正在用 C# 编写一个应用程序(对于我的一个 friend 来说,这是非常基础的),但是我有一个 StreamWriter 对象,它在 C: 中创建一个本地文件。我必须以管理员身份运行,它工作正常,
我已经安装了 Centos5,安装了 Web 服务器并设置了虚拟主机。主机几乎是这样设置的: > adduser user1 > mkdir -p /home/user1/public_html/do
我正从 MySQL 迁移到 PostgreSQL,但在用户权限方面遇到了瓶颈。我习惯于使用以下命令为用户分配对数据库所有表的所有权限: # MySQL grant all privileges on
我试图为特定用户授予对 UNC 路径的 NTFS 权限,但我看到了不同的行为,具体取决于 UNC 路径。下面是我用来授予权限的代码(来自 MSDN)以及每种情况下的结果, static void Gi
我是一名优秀的程序员,十分优秀!