1、简介

Identity Server4支持用户名密码模式,允许调用客户端使用用户名密码来获得访问Api资源(遵循Auth 2.0协议)的Access Token,MS可能考虑兼容老的系统,实现了这个功能,但是不建议这么做.

2、实战一服务端配置

接着Identity Server4学习系列三的基础上,直接扩展里面的项目代码,让服务端同时支持密钥认证和用户名密码认证

第一步:扩展ThirdClients类,如下:

    /// <summary>
/// 配置可以访问IdentityServer4 保护的Api资源模型的第三方客户端
/// 配置客户端访问的密钥
/// </summary>
public class ThirdClients
{
public static IEnumerable<Client> GetClients()
{
return new List<Client>()
{
new Client()
{
//客户端的唯一Id,客户端需要指定该ClientId才能访问
ClientId = $"client", //no interactive user, use the clientid/secret for authentication
//使用客户端密钥进行认证
AllowedGrantTypes = GrantTypes.ClientCredentials, // 认证密钥,客户端必须使用secret密钥才能成功访问
ClientSecrets =
{
//用Sha256对"secret"进行加密
new Secret("secret".Sha256())
}, // scopes that client has access to
//如果客户端的密钥认证成功,限定该密钥可以访问的Api范围
AllowedScopes = { "api1" }
},
//添加支持用户名密码模式访问的客户端类型
new Client()
{
ClientId = "userPwd.client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
} /// <summary>
/// 配置可以访问IdentityServer4 保护的Api资源模型的第三方客户端
/// 使用用户名密码模式
/// </summary>
/// <returns></returns>
public static List<TestUser> GetUsers()
{
return new List<TestUser>()
{
new TestUser()
{
SubjectId = "",
Username = "alice",
Password = "password"
}
};
}
}

第二步:注册TestUser到Identity Server4,修改StartUp文件如下:

    public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
//优雅的链式编程
//注入Identity Server4服务到DI容器中
services.AddIdentityServer()
//注入临时签名凭据到DI容器,后期可用签名证书的密钥替换,用于生成零时密钥
.AddDeveloperSigningCredential()
//注入需要受Identity Server4保护的Api资源添注入到DI容器中 -内存级别
.AddInMemoryApiResources(Apis.GetApiResources())
//注入需要访问受Identity Server4保护的Api资源的客户端(密钥模式)注入到DI容器中 -内存级别
.AddInMemoryClients(ThirdClients.GetClients())
//注入需要访问受Identity Server4保护的Api资源的客户端(用户名密码访问模式)注入到DI容器中 -内存级别
.AddTestUsers(ThirdClients.GetUsers()); //注入基本的MVC服务
services.AddMvcCore()
//注入MVC的认证服务,对应控制器的Authorize特性
.AddAuthorization()
//注入MVC格式化程序,对应JsonResult等等的格式化操作,主要用于控制器返回值的格式化操作
.AddJsonFormatters(); //注入身份认证服务,设置Bearer为默认方案
services.AddAuthentication("Bearer")
//注入并配置Bearer为默认方案的基本参数
.AddIdentityServerAuthentication(options =>
{
//设置令牌的发布者
options.Authority = "http://localhost:5000";
//设置Https
options.RequireHttpsMetadata = false;
//需要认证的api资源名称
options.ApiName = "api1";
});
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//如果当前时开发者模式
if (env.IsDevelopment())
{
//从管道中捕获同步和异步System.Exception实例并生成HTML错误响应。
app.UseDeveloperExceptionPage();
} //将IdentityServer 4服务注入到管道模型中(对应上面的IdentityServer 4服务的配置)
app.UseIdentityServer(); //将认证服务通过Microsoft.AspNetCore.Authentication.AuthenticationMiddleware中间件
//注入到管道模型中(对应上面认证服务的配置)
app.UseAuthentication(); //将mvc添加到Microsoft.AspNetCore.Builder.IApplicationBuilder请求执行中(对应上的MVC配置)
app.UseMvc();
}
}

ok,到这一步,Identity Server4服务端配置完成!

3、实战一客户端发起调用

调用代码如下:

    class Program
{
static void Main(string[] args)
{
Request();
Console.ReadKey();
} async static void Request()
{
//请求Identity Server4服务
var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
var tokenClient = new TokenClient(disco.TokenEndpoint, "userPwd.client", "secret");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("alice", "password", "api1"); if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
//通过Identity Server4的认证过后,拿到AccessToken
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
var response = await client.GetAsync("http://localhost:5000/identity");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
//认证成功,输出Identity控制器的返回值
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
}
}

ok,使用用户名加密钥模式,访问Api成功拿到Api返回值,注意密钥任然需要给,因为这个密钥是用与给Token加密的,而用户名和密码无非是继续加一了一层认证,如果密钥认证成功,必须进行用户名和密码的认证,两者必须同时认证成功,才能成功的发起Api的调用.

用户名和密码必须和服务端给定的一致,否则客户端会报这个错:

无效的授权.

至此,用户名密码加密钥模式介绍完毕!

Identity Server4学习系列四之用户名密码获得访问令牌的更多相关文章

  1. Identity Server4学习系列三

    1.简介 在Identity Server4学习系列一和Identity Server4学习系列二之令牌(Token)的概念的基础上,了解了Identity Server4的由来,以及令牌的相关知识, ...

  2. Identity Server4学习系列一

    一.前言 今天开始学习Identity Server4,顺便了解下.Net Core,以便于完善技术栈,最主要的是要跟上.Net的发展潮流,顺便帮助各位整理下官方文档,加上一些我自己对他的理解. 这是 ...

  3. Identity Server4学习系列二之令牌(Token)的概念

    1.简介 通过前文知道了Identity Server4的基本用途,现在必须了解一些实现它的基本细节. 2.关于服务端生成Token令牌 头部(Header): { “typ”: “JWT”, //t ...

  4. scrapy爬虫学习系列四:portia的学习入门

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  5. DocX开源WORD操作组件的学习系列四

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  6. .net reactor 学习系列(四)---.net reactor应用场景

    原文:.net reactor 学习系列(四)---.net reactor应用场景         前面已经学习了.net reactor一些基础知识,现在准备学习下实际的应用场景,只是简单的保护和 ...

  7. MVC3+EF4.1学习系列(四)----- ORM关系的处理

    上篇文章 终于把基础的一些操作写完了 但是这些都是单表的处理 而EF做为一个ORM框架  就必须点说说对于关系的处理 处理好关系 才能灵活的运用EF 关于关系的处理 一般就是  一对一   一对多  ...

  8. Vue学习系列(四)——理解生命周期和钩子

    前言 在上一篇中,我们对平时进行vue开发中遇到的常用指令进行归类说明讲解,大概已经学会了怎么去实现数据绑定,以及实现动态的实现数据展示功能,运用指令,可以更好更快的进行开发.而在这一篇中,我们将通过 ...

  9. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

随机推荐

  1. IntelliJ IDEA 2017版 spring-boot搭建拦截器

    1.建立一个springboot-web项目 https://www.cnblogs.com/liuyangfirst/p/8298588.html 2.加入过滤接口 public class Log ...

  2. .Net实现Windows服务安装完成后自动启动的两种方法

    考虑到部署方便,我们一般都会将C#写的Windows服务制作成安装包.在服务安装完成以后,第一次还需要手动启动服务,这样非常不方便. 方法一:在安装完成事件里面调用命令行的方式启动服务 此操作之前要先 ...

  3. [指南] 15分钟学会MySQL(Linux版)

    原文链接:http://www.mysqlpub.com/thread-348-1-1.html 原创出处:MySQLpub.com  , 作者:kider  ,转载请注明作者和出处,并不能用于商业用 ...

  4. Servlet Life Cycle

    Servlet Life Cycle http://docs.oracle.com/javaee/5/tutorial/doc/bnafi.html Servlet Filters and Event ...

  5. 2.2.11同步synchronized方法无限等待与解决

    同步方法容易造成死循环. package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { s ...

  6. 自己写一个chrome扩展程序 - 右键菜单扩展

    最近在学习Spring,心想dotnet如何实现类似形式呢.于是想认真学习Casetle组件,发现没有书籍!而spring的书多得很.于是只好找网上教程了.发现系统的文章不多.Terrylee好多文章 ...

  7. maven依赖管理

    maven依赖管理 1.依赖范围   (依赖相当于java中的import  是否需要导入别的jar包) 使用控制依赖与三种classpath(编译期,测试时期,运行时期)的关系 complie    ...

  8. java基本语法、标识符、关键字

    基本语法 编写Java程序时,应注意以下几点: 大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的. 类名:对于所有的类来说,类名的首字母应该大写.如果类名由若干单词组 ...

  9. 差值的再议-Hermite差值

    1. 插值法 插值法又称“内插法”,是利用函数f (x)在某区间中已知的若干点的函数值,作出适当的特定函数,在区间的其他点上用这特定函数的值作为函数f (x)的近似值,这种方法称为插值法. 如果这特定 ...

  10. HDU3480_区间DP平行四边形优化

    HDU3480_区间DP平行四边形优化 做到现在能一眼看出来是区间DP的问题了 也能够知道dp[i][j]表示前  i  个节点被分为  j  个区间所取得的最优值的情况 cost[i][j]表示从i ...