IdentityServer4专题之六:Resource Owner Password Credentials



实现代码:
(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的更多相关文章
- IdentityServer4之Resource Owner Password Credentials(资源拥有者密码凭据许可)
IdentityServer4之Resource Owner Password Credentials(资源拥有者密码凭据许可) 参考 官方文档:2_resource_owner_passwords ...
- asp.net core IdentityServer4 实现 resource owner password credentials(密码凭证)
前言 OAuth 2.0默认四种授权模式(GrantType) 授权码模式(authorization_code) 简化模式(implicit) 密码模式(resource owner passwor ...
- IdentityServer4 之 Resource Owner Password Credentials 其实有点尴尬
前言 接着IdentityServer4的授权模式继续聊,这篇来说说 Resource Owner Password Credentials授权模式,这种模式在实际应用场景中使用的并不多,只怪其太开放 ...
- 不要使用Resource Owner Password Credentials
不要使用Resource Owner Password Credentials 文章链接在这里 前言 最近公司项目在做一些重构,因为公司多个业务系统各自实现了一套登录逻辑,比较混乱.所以,现在需要做一 ...
- 使用Resource Owner Password Credentials Grant授权发放Token
对应的应用场景是:为自家的网站开发手机 App(非第三方 App),只需用户在 App 上登录,无需用户对 App 所能访问的数据进行授权. 客户端获取Token: public string Get ...
- 基于 IdentityServer3 实现 OAuth 2.0 授权服务【密码模式(Resource Owner Password Credentials)】
密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码.客户端使用这些信息,向"服务商提供商"索要授权 ...
- 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】
适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...
- OAuth2.0学习(1-6)授权方式3-密码模式(Resource Owner Password Credentials Grant)
授权方式3-密码模式(Resource Owner Password Credentials Grant) 密码模式(Resource Owner Password Credentials Grant ...
- ABP中使用OAuth2(Resource Owner Password Credentials Grant模式)
ABP目前的认证方式有两种,一种是基于Cookie的登录认证,一种是基于token的登录认证.使用Cookie的认证方式一般在PC端用得比较多,使用token的认证方式一般在移动端用得比较多.ABP自 ...
随机推荐
- 使用Dotfunsctor
设置Disable Control Flow.Disable Renaming.Disable String Encryption 为no,no为开启该功能 设置加密后输出的路i经 选择需要加密的ex ...
- AP2800无法放出SSID?
实际的无线网络中,有时候由于对无线设备的datasheet不是很了解,可能会以旧的知识去判断一些故障.在思科的较新的AP型号中:例如AP2800&AP3800等,有时候发现它们可正常的注册到W ...
- 洛谷 P3371 【模板】单源最短路径(弱化版) && dijkstra模板
嗯... 题目链接:https://www.luogu.org/problem/P3371 没什么好说的,这是一个最短路的模板,这里用的dijkstra做的... 注意: 1.dijkstra和邻接表 ...
- 测试Nginx中location的优先级!(重点)
location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ~ 开头表示区分大小写的正则匹配 ~* 开头表示不区分大小写的正则匹配 ^~ 开头表示uri以某个常规字符串开头 ...
- dubbo的dispatcher设置原理
在上回<Dubbo源代码实现六>中我们已经了解到,对于Dubbo集群中的Provider角色,有IO线程池(默认无界)和业务处理线程池(默认200)两个线程池,所以当业务的并发比较高,或者 ...
- 2020.02.28 Linux 命令
Cat 语法格式 cat [-AbeEnstTuv] [--help] [--version] fileName 参数说明: -n 或 --number:由 1 开始对所有输出的行数编号. -b ...
- 笔记-mongodb数据操作
笔记-mongodb数据操作 1. 数据操作 1.1. 插入 db.COLLECTION_NAME.insert(document) 案例: db.inventory.insertOn ...
- 端口打开和关闭do while
;Author : Bing Song ;// ;Usage: modify “logfile" according to actual drictory getdir logdir #获取 ...
- 在spring boot中使用jasypt对配置文件中的敏感字符串加密
在spring boot的配置文件application.property(application.yml)文件中常常配置一些密码类的字符,如果用明文则很容易被盗用,可以使用jasypt在配置密码的地 ...
- C++常量表达式、const、constexpr(C++11新增)的区别
常量表达式是指值不会改变且在编译过程中就能够得到计算结果的表达式,能在编译时求值的表达式. 程序先编译再运行: 在编译阶段, 编译器将在编译过程中把用到该常量的地方都全都替换为 常量的值. 但是常量 ...