1.  修改 Config.cs

using System.Collections;
using System.Collections.Generic;
using IdentityServer4.Models;
using IdentityServer4.Test;//测试的时候使用 namespace IdentityServiceSample
{
public class Config
{
public static IEnumerable<ApiResource> GetResource()
{ return new List<ApiResource>(){
new ApiResource("api","my api")
}; } public static IEnumerable<Client> GetClients()
{ return new List<Client>(){
new Client(){
ClientId="client",
//客户端模式
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets={new Secret("secret".Sha256())},
AllowedScopes={"api"}
},
new Client(){
ClientId="pwdClient",
//OAuth密码模式
AllowedGrantTypes=GrantTypes.ResourceOwnerPassword, ClientSecrets={new Secret("secret".Sha256())}, AllowedScopes={"api"}
}
};
} //TODO 实际使用可以读取Db
public static List<TestUser> GetTestUser()
{
return new List<TestUser>{
new TestUser{
SubjectId="",
Username="sunxuchu",
Password=""
}
};
}
}
}

2. 修改 Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using IdentityServer4; namespace IdentityServiceSample
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//1.注入IdentityServer
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResource())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetTestUser()); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} // 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())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
} // app.UseHttpsRedirection();
// app.UseMvc(); //2. 注册IdentityServer
app.UseIdentityServer();
}
}
}

3. 使用PostMan 调用调试

请求参数:

client_id:pwdClient
client_secret:secret
grant_type:password
password:123456
username:sunxuchu

4. 密码模式HttpClient 模拟请求

using System;
using System.Net.Http;
using IdentityModel.Client; namespace PwdClient
{
class Program
{
static void Main(string[] args)
{
try
{
new Program().GetAsync();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
} Console.ReadKey();
} public async void GetAsync()
{
var diso = await DiscoveryClient.GetAsync("http://localhost:5003");
if (diso.IsError)
{
System.Console.WriteLine("diso.Error");
}
var tokenClient = new TokenClient(diso.TokenEndpoint, "pwdClient", "secrt");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("sunxuchu","");
if (tokenResponse.IsError)
{
System.Console.WriteLine(tokenResponse.Error);
}
else
{
System.Console.WriteLine(tokenResponse.Json);
} using (var httpClient = new HttpClient())
{
httpClient.SetBearerToken(tokenResponse.AccessToken);
var response = await httpClient.GetAsync("http://localhost:5001/api/values");
if (response.IsSuccessStatusCode)
{
System.Console.WriteLine(await response.Content.ReadAsStringAsync());
}
} }
}
}

IdentityServer4 密码模式实现的更多相关文章

  1. IdentityServer4 密码模式认证

     授权服务器设置 添加用户 添加测试用户,也可以从数据库查 public static List<TestUser> GetTestUser() { return new List< ...

  2. IdentityServer4密码模式接入现有用户数据表

    具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...

  3. Core篇——初探IdentityServer4(客户端模式,密码模式)

    Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...

  4. 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现

    本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...

  5. asp.net core 使用identityServer4的密码模式来进行身份认证(一)

    IdentityServer4是ASP.NET Core的一个包含OpenID和OAuth 2.0协议的框架.具体Oauth 2.0和openId请百度. 前言本博文适用于前后端分离或者为移动产品来后 ...

  6. IdentityServer4 实现OAuth2.0四种模式之密码模式

    接上一篇:IdentityServer4 实现OAuth2.0四种模式之客户端模式,这一篇讲IdentityServer4 使用密码模式保护API访问. 一,IdentityServer配置 1,添加 ...

  7. IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)

    一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...

  8. IdentityServer4授权模式应用场景

    OpenID 和 OAuth 的区别 IdentityServer4,NET Core下的安全框架 客户端模式(Client Credentials) 密码模式(resource owner pass ...

  9. ASP.NET Core分布式项目-2.oauth密码模式identity server4实现

    源码下载 这里根据<ASP.NET Core分布式项目-1.IdentityServer4登录中心>的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全 在WebAp ...

随机推荐

  1. css样式记忆

    text-indent: 2em;   //开头空两格: display : none;    //隐藏元素 background:#CCC;  //背景颜色 background: url(imag ...

  2. sqlserver中怎么查询字段为空的记录

    sqlserver中怎么查询字段为空的记录的两种方法: 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https://cnblogs.com/qzf/

  3. 2018.09.16 bzoj3757: 苹果树(树上莫队)

    传送门 一道树上莫队. 先用跟bzoj1086一样的方法给树分块. 分完之后就可以莫队了. 但是两个询问之间如何转移呢? 感觉很难受啊. 我们定义S(u,v)" role="pre ...

  4. 2018.08.22 NOIP模拟 string(模拟)

    string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...

  5. yii框架场景的用法

    1.在 model 里面定义一下场景 类名必须是 scenarios() public function scenarios() { return [ 'create' => ['title', ...

  6. simpson法计算arctan(1)-即pi/4

    对1/(1+x^2) 进行0到1的积分即使pi/4; 采用simpson方法 Func<double,double> func=(x)=>{ return 1/(1+ Math.Po ...

  7. angularjs写公共方法

    'use strict'; angular.module('fast-westone') .factory('commonUtilService', function () { return { /* ...

  8. 继承方法-->最终模式

    function inherit(Target,Origin){ function F(){}; F.prototype = Origin.prototype; // Targrt.prototype ...

  9. android中Actionbar详解

    1.什么是Action BarAction Bar被认为是新版Android系统中最重要的交互元素,在程序运行中一直置于顶部,主要起到的作用在于:1)突出显示一些重要操作(如“最新”.“搜索”等)2) ...

  10. hdu 5023 线段树+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=5023 在片段上着色,有两种操作,如下: 第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c . 第 ...