c# - 如何以面向 future 的方式从 SPA 正确获取 IDS4 中的 token ?

标签 c# token identityserver4

我读入了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 方面下一步该做什么。在我得到的对象中,有 AccessTokenIdentityTokenRefreshToken 等,我不知道该依赖哪个关于它们之间差异的文档含糊不清。

随着通话的进行,我收到一条错误消息,指出客户端未经授权且 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/

相关文章:

c# - 是否应该在接口(interface)中声明所有公共(public)方法?

c# - SignalR 或 Redis pubsub 用于 Azure 上的缩放发布者和缩放订阅者

language-agnostic - 为自动完成场景表示语言标记的最佳方式

IdentityServer4,IResourceStore.GetAllResources()

c# - 无法使用 EntityFramework 教程添加迁移

angular - IdentityServer 外部身份验证提供程序 - 身份验证回调 - 重定向 - 400 错误请求

c# - 您如何满足 WebApi 查询字符串中的 AND 和 OR 搜索?

c# - knockout 下拉列表数据绑定(bind)在 ajax 调用中不起作用

namespaces - JAX-WS 在 token 的签名中添加命名空间

node.js - 具有 jwt 授权的 get 方法返回空对象,我们是否需要解码 jwt token ?