Json从IdentityServer4验证用户

时间:2017-03-01 19:32:10

标签: c# json oauth-2.0 openid-connect identityserver4

我设置了IdentityServer4,它实现了oAuth和OpenId Connect,Simple Implementation看起来像这样

services.AddIdentityServer()
    .AddTemporarySigningCredential()
    .AddInMemoryIdentityResources(Config.GetIdentityResources())
    .AddInMemoryApiResources(Config.GetApiResources())
    .AddInMemoryClients(Config.GetClients())
    .AddTestUsers(Config.GetTestUsers());

我们的客户端设置如下:

new Client
{
    ClientId = "oauthClient",
    ClientName = "Example Client Credentials Client Application",
    AllowedGrantTypes = GrantTypes.ClientCredentials,
    ClientSecrets = new List<Secret> {
        new Secret("superSecretPassword".Sha256())},
           AllowedScopes = new List<string> {"customAPI.read"}
    }

我正在试图弄清楚如何为用户创建登录请求 我将这个json传递到帖子正文中以获取对身份验证令牌的访问权限

{
   grant_type:client_credentials,
   scope=customAPI.read,
   client_id=oauthClient
   client_secret=superSecretPassword
}

我正在寻找一种方法来做到这一点,但假设我有一个

,则传递用户信息

username: admin password: root

我需要在json中修改哪些参数才能以用户身份登录?如何传递用户名,密码以及什么是我的Grant_Type?

1 个答案:

答案 0 :(得分:0)

我的问题是设置我的客户端,我的客户端只接受授权类型的客户端凭据,我还需要包含ResourceOwnerPassword。

我需要在客户端更改我的授权类型,看起来像这样

new Client
{
    ClientId = "oauthClient",
    ClientName = "Example Client Credentials Client Application",
    AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
    ClientSecrets = new List<Secret> {
        new Secret("superSecretPassword".Sha256())},
    AllowedScopes = new List<string> {"customAPI.read"}
}

现在我们可以像这样形成Post Body Json

url: localhost/connect/token
Content-Type: application/x-www-form-urlencoded,
data: {
    grant_type: 'password',
    scope: 'customAPI.read',
    client_id: 'oauthClient',
    client_secret:'superSecretPassword',
    username:'admin',
    password: 'root'
}

修改

根据IdentityServer4 Documentation

显然不再使用ResourceOwnerPassword
  

规范建议仅对“受信任”(或遗留)应用程序使用资源所有者密码授予。一般来说,当您想要对用户进行身份验证并请求访问令牌时,通常会更好地使用其中一个交互式OpenID Connect流程。