我在IDS4's tutorial中读到以下是获取令牌的方法。
var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");
if (tokenResponse.IsError) { ... }
Console.WriteLine(tokenResponse.Json);
然而,它被intellisense表示它即将被宣布为过时,所以我检查了身份模型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
});
我不确定在生成令牌方面下一步该做什么。在我得到的对象中,有AccessToken,IdentityToken,RefreshToken等,我很困惑哪些依赖,因为关于它们之间的差异的文档是模糊的。
随着调用现在,我得到一个错误,说客户端是未授权的,令牌为空。客户端声明如下。
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"
}
}
这令人困惑,因为文档需要涵盖很多不同的情况。你最好专注于implicit flow,因为这是SPA应用的一个很好的候选者。
IdentityServer提供了一个示例和文档,请查看JavaScriptClient。
接下来最好的事情是阅读specs。这是源头。 IdentityServer实现这些规范。此外,您还可以阅读IdentityServer的文档。
这应该有助于您了解使用令牌的时间和地点。这取决于客户端的类型和所选择的流程。你可以找到有关那个here的信息。
简而言之,令牌的含义:
根据流程,响应可能不同。
有关如何使用访问令牌使用javascript访问资源(api)的文档,请阅读this。当访问令牌到期时,您将需要一个新令牌,而无需要求用户再次登录。由于隐式流不支持刷新令牌,因此您可以实现here所述的静默令牌续订。
关于过时的代码,TokenClient
被替换为HttpClient。您现在可以调用HttpClient的扩展名,而不是设置TokenClient。这是代码改进,不会影响流的实现。
而不是使用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"
});