我读入了IDS4's tutorial以下是获取token的方法。
var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");
if (tokenResponse.IsError) { ... }
Console.WriteLine(tokenResponse.Json);
但是,智能感知表明它即将被声明为过时,所以我查看了身份模型的文档 password grant type建议如下。
var response = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
{
Address = "https://localhost:44300/connect/token",
ClientId = "spa",
ClientSecret = "spa_secret",
Scope = "MemberApi",
UserName = user,
Password = pass
});
我不确定在生成 token 方面下一步该做什么。在我得到的对象中,有 AccessToken、IdentityToken、RefreshToken 等,我不知道该依赖哪个关于它们之间差异的文档含糊不清。
随着通话的进行,我收到一条错误消息,指出客户端未经授权且 token 为 null。客户端声明如下。
new Client
{
ClientId = "spa",
ClientSecrets = { new Secret("spa_secret".Sha256()) },
ClientName = "SPA client",
ClientUri = "http://identityserver.io",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:5000/security/credentials" },
PostLogoutRedirectUris = { "http://localhost:5000/index.html" },
AllowedCorsOrigins = { "http://localhost:5000", "https://localhost:44300" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"MemberApi",
"MemberApi.full",
"MemberApi.limited"
}
}
最佳答案
不使用 TokenClient,目的是在将来使用扩展方法(在 HttpClient 上)。
与 TokenClient 类具有相似功能的扩展方法是 RequestClientCredentialsTokenAsync
按如下方式使用:
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://auth.myserver.com:5011");
var tokenResponse = await client.RequestClientCredentialsTokenAsync(
new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "MyAppClient",
ClientSecret = "secret",
Scope = "api1"
});
关于c# - 如何以面向 future 的方式从 SPA 正确获取 IDS4 中的 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806658/