实现代码:

(1)IdentityServer4授权服务器代码:

public static class Config

{  public static IEnumerable<IdentityResource> GetIdentityResources()  //对身份资源的配置

{

return new IdentityResource[]

{

new IdentityResources.OpenId(),  //此项必选

new IdentityResources.Profile(),

new IdentityResources.Email(),

new IdentityResources.Phone(),

new IdentityResources.Address(),

};

}

public static IEnumerable<ApiResource> GetApis()  //对API的配置

{

return new ApiResource[]

{

new ApiResource("api1", "My API #1")

};

}

public static IEnumerable<Client> GetClients()  //配置可访问客户

{

return new[]

{

// client credentials flow client

new Client

{

ClientId = "console client",

ClientName = "Client Credentials Client",

AllowedGrantTypes = GrantTypes.ClientCredentials,

ClientSecrets = { new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256()) },

AllowedScopes = {"api1" }

},

new Client

{

ClientId="password client",

AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,

ClientSecrets={new Secret("password secret".Sha256())},

AllowedScopes={"api1",IdentityServerConstants.StandardScopes.OpenId,

IdentityServerConstants.StandardScopes.Profile,

IdentityServerConstants.StandardScopes.Address,

IdentityServerConstants.StandardScopes.Phone,

IdentityServerConstants.StandardScopes.Email

}

}

};

}

}

(2)对API的配置同ClientCredential,完全相同

(3)客户端代码,客户端还是需要NUGET安装IdentityModel库

static async Task Main(string[] args)

{

//Discovery endpoint

Console.WriteLine("Hello World!");

var client = new HttpClient();

var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");

if (disco.IsError)

{

Console.WriteLine(disco.Error);

return;

}

//request access token

var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest

{

Address=disco.TokenEndpoint,

ClientId="password client",

ClientSecret="password secret",

Scope="api1 openid profile email phone address",

UserName="bob",   //此处设置的密码应在identityserver4的TestUsers类中有定义

Password="bob"

});

if(tokenResponse.IsError)

{

Console.WriteLine(tokenResponse.Error);

return;

}

Console.WriteLine(tokenResponse.Json.ToString());

var clientApi = new HttpClient();

clientApi.SetBearerToken(tokenResponse.AccessToken);

var apiResponse =await clientApi.GetAsync("http://localhost:5002/api/values");

if (!apiResponse.IsSuccessStatusCode)

{

Console.WriteLine(apiResponse.StatusCode);

return;

}

var content = await apiResponse.Content.ReadAsStringAsync();

Console.WriteLine(content);

var clientUserInfo = new HttpClient();

clientUserInfo.SetBearerToken(tokenResponse.AccessToken);

var userinfoResponse = await clientUserInfo.GetAsync(disco.UserInfoEndpoint);

if(!userinfoResponse.IsSuccessStatusCode)

{

Console.WriteLine(userinfoResponse.StatusCode);

return;

}

var contentuserinfo = await userinfoResponse.Content.ReadAsStringAsync();

Console.WriteLine(contentuserinfo);

Console.WriteLine("llll");

}

}

IdentityServer4专题之六:Resource Owner Password Credentials的更多相关文章

  1. IdentityServer4之Resource Owner Password Credentials(资源拥有者密码凭据许可)

    IdentityServer4之Resource Owner Password Credentials(资源拥有者密码凭据许可) 参考 官方文档:2_resource_owner_passwords ...

  2. asp.net core IdentityServer4 实现 resource owner password credentials(密码凭证)

    前言 OAuth 2.0默认四种授权模式(GrantType) 授权码模式(authorization_code) 简化模式(implicit) 密码模式(resource owner passwor ...

  3. IdentityServer4 之 Resource Owner Password Credentials 其实有点尴尬

    前言 接着IdentityServer4的授权模式继续聊,这篇来说说 Resource Owner Password Credentials授权模式,这种模式在实际应用场景中使用的并不多,只怪其太开放 ...

  4. 不要使用Resource Owner Password Credentials

    不要使用Resource Owner Password Credentials 文章链接在这里 前言 最近公司项目在做一些重构,因为公司多个业务系统各自实现了一套登录逻辑,比较混乱.所以,现在需要做一 ...

  5. 使用Resource Owner Password Credentials Grant授权发放Token

    对应的应用场景是:为自家的网站开发手机 App(非第三方 App),只需用户在 App 上登录,无需用户对 App 所能访问的数据进行授权. 客户端获取Token: public string Get ...

  6. 基于 IdentityServer3 实现 OAuth 2.0 授权服务【密码模式(Resource Owner Password Credentials)】

    密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码.客户端使用这些信息,向"服务商提供商"索要授权 ...

  7. 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】

    适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...

  8. OAuth2.0学习(1-6)授权方式3-密码模式(Resource Owner Password Credentials Grant)

    授权方式3-密码模式(Resource Owner Password Credentials Grant) 密码模式(Resource Owner Password Credentials Grant ...

  9. ABP中使用OAuth2(Resource Owner Password Credentials Grant模式)

    ABP目前的认证方式有两种,一种是基于Cookie的登录认证,一种是基于token的登录认证.使用Cookie的认证方式一般在PC端用得比较多,使用token的认证方式一般在移动端用得比较多.ABP自 ...

随机推荐

  1. Centos5.5+LAMP环境

    Note:如果网络正常,apache服务正常,仍然不能访问网页.需要检查linux 防火墙是否关闭. ( 先重新启动防火墙 service iptables start 然后输入配置防火墙的命令并查看 ...

  2. [03] Recursive Function递归应用

    递归应用 1.理解 百科:一种计算过程,如果其中每一步都要用到前一步或前几步的结果,称为递归的: 理解:函数调用自己的过程,这类函数处理的事情具有重复性,处理此类实行可用while或者for,但结构上 ...

  3. java 使用poi 导入Excel 数据到数据库

    由于我个人电脑装的Excel是2016版本的,所以这地方我使用了XSSF 方式导入 . 1先手要制定一个Excel 模板 把模板放入javaWeb工程的某一个目录下如图: 2模板建好了后,先实现模板下 ...

  4. linux-命令行快捷方式使用

    CTRL+P 命令向上翻滚 CTRL+N  命令向下翻滚 CTRL+U 命令行中删除光标前面的所有字符 CTRL+D 命令行中删除光标后面的一个字符 CTRL+H 命令行中删除光标前面的一个字符 CT ...

  5. redhat7.6 DNS配置正向解析

    1.安装DNS服务 yum install bind yum install bind-chroot 安装完的配置文件/etc/named.conf 启动systemctl start named.s ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:缩写

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 缩写</title> <lin ...

  7. c# 字符串比较优化

    一,优化举例 二,浅谈StringComparison 三,C# CultureInfo 类 各国语言对应的区域性名称 一,优化举例 我们在写程序的时候,经常会用到字符串对比.例如:if(IsChec ...

  8. Python数据类型-8 集合set

    集合set set集合是一个无序不重复元素的集,基本功能包括关系测试和消除重复元素.集合使用大括号({})框定元素,并以逗号进行分隔.但是注意:如果要创建一个空集合,必须用 set() 而不是 {} ...

  9. CVPR 2019 行人检测新思路:

    CVPR 2019 行人检测新思路:高级语义特征检测取得精度新突破 原创: CV君 我爱计算机视觉 今天 点击我爱计算机视觉置顶或标星,更快获取CVML新技术 今天跟大家分享一篇昨天新出的CVPR 2 ...

  10. keil条件断点

    http://www.keil.com/support/man/docs/uv4/uv4_db_dbg_breakpnts.htm Target Device does not support con ...