首页 > 解决方案 > 如何使用从 IdentityServer 获取的令牌授权用户?

问题描述

我已经使用以下配置创建了一个 IdentityServer 项目( B ):

new Client
{
    ClientId = "ro.client",
    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
    ClientSecrets =
    {
        new Secret("secret".Sha256())
    },
    AllowedScopes = { "api1" }
}

我有项目 B 的自定义登录页面,当用户输入他/她的用户和密码时,我将此信息发送到项目 B 的后端,通过此代码我可以从身份服务器获取令牌 - 项目 A:

var client = new HttpClient();
        var disco = await client.GetDiscoveryDocumentAsync(_configuration["App:Authority"]);
        if (disco.IsError)
        {
            throw new System.Exception("get info from identity server is failed.");
        }

        var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
        {
            Address = disco.TokenEndpoint,
            ClientId = "ro.client",
            ClientSecret = "secret",

            UserName = "bob",
            Password = "Pass123$",
            Scope = "api1"
        });

        var accessToken = tokenResponse.AccessToken;

现在我想要实现的是,我以某种方式使该用户获得授权并将令牌保存在 cookie 中,因此当该用户调用另一个具有授权属性的操作时,不会将他/她重定向到登录页面。我怎样才能做到这一点?

标签: .net-coreidentityserver4authorize

解决方案


推荐阅读