系列目录:

DotNetOpenAuth实践系列(源码在这里)

DotNetOpenAuth是OAuth2的.net版本,利用DotNetOpenAuth我们可以轻松的搭建OAuth2验证服务器,不废话,下面我们来一步步搭建验证服务器

本次搭建环境:

.net4.5.1 ,DotNetOpenAuth v5.0.0-alpha3,MVC5

一、环境搭建

  1、新建一个空的VS解决方案

  2、添加验证服务器项目,项目选择MVC,不要自带的身份验证

  3、使用Nuget添加DotNetOpenAuth v5.0.0-alpha3

输入DotNetOpenAuth 安装DotNetOpenAuth v5.0.0-alpha3

添加完成后

二、编写DotNetOpenAuth 验证服务器关键代码,实现功能

  1、添加AuthorizationServerConfiguration.cs

这里的配置是为了添加方便管理,其实可以不用这个类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Security.Cryptography.X509Certificates;
 5 using System.Web;
 6
 7 namespace IdefavAuthorizationServer.Code
 8 {
 9     /// <summary>
10     /// 验证服务器配置
11     /// </summary>
12     public class AuthorizationServerConfiguration
13     {
14         /// <summary>
15         /// 构造函数
16         /// </summary>
17         public AuthorizationServerConfiguration()
18         {
19             TokenLifetime = TimeSpan.FromMinutes(5);
20         }
21
22         /// <summary>
23         /// 签名证书
24         /// </summary>
25         public X509Certificate2 SigningCertificate { get; set; }
26
27         /// <summary>
28         /// 加密证书
29         /// </summary>
30         public X509Certificate2 EncryptionCertificate { get; set; }
31
32         /// <summary>
33         /// Token有效时间
34         /// </summary>
35         public TimeSpan TokenLifetime { get; set; }
36     }
37 }

2、实现IClientDescription接口

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using DotNetOpenAuth.Messaging;
 6 using DotNetOpenAuth.OAuth2;
 7
 8 namespace IdefavAuthorizationServer.Code
 9 {
10     public class Client : IClientDescription
11     {
12         /// <summary>
13         /// 客户端名称client_id
14         /// </summary>
15         public string Name { get; set; }
16
17         /// <summary>
18         /// 客户端类型
19         /// </summary>
20         public int ClientType { get; set; }
21
22         /// <summary>
23         /// 回调URL
24         /// </summary>
25         public string Callback { get; set; }
26
27         public string ClientSecret { get; set; }
28
29
30         Uri IClientDescription.DefaultCallback
31         {
32             get { return string.IsNullOrEmpty(this.Callback) ? null : new Uri(this.Callback); }
33         }
34
35
36         ClientType IClientDescription.ClientType
37         {
38             get { return (ClientType)this.ClientType; }
39         }
40
41
42         bool IClientDescription.HasNonEmptySecret
43         {
44             get { return !string.IsNullOrEmpty(this.ClientSecret); }
45         }
46
47
48         bool IClientDescription.IsCallbackAllowed(Uri callback)
49         {
50             if (string.IsNullOrEmpty(this.Callback))
51             {
52                 // No callback rules have been set up for this client.
53                 return true;
54             }
55
56             // In this sample, it's enough of a callback URL match if the scheme and host match.
57             // In a production app, it is advisable to require a match on the path as well.
58             Uri acceptableCallbackPattern = new Uri(this.Callback);
59             if (string.Equals(acceptableCallbackPattern.GetLeftPart(UriPartial.Authority), callback.GetLeftPart(UriPartial.Authority), StringComparison.Ordinal))
60             {
61                 return true;
62             }
63
64             return false;
65         }
66
67
68         bool IClientDescription.IsValidClientSecret(string secret)
69         {
70             return MessagingUtilities.EqualsConstantTime(secret, this.ClientSecret);
71         }
72
73
74     }
75 }

3、实现IAuthorizationServerHost接口

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Security.Cryptography;
 5 using System.Web;
 6 using DotNetOpenAuth.Messaging.Bindings;
 7 using DotNetOpenAuth.OAuth2;
 8 using DotNetOpenAuth.OAuth2.ChannelElements;
 9 using DotNetOpenAuth.OAuth2.Messages;
10
11 namespace IdefavAuthorizationServer.Code
12 {
13     public class IdefavAuthorizationServerHost : IAuthorizationServerHost
14     {
15         /// <summary>
16         /// 配置
17         /// </summary>
18         private readonly AuthorizationServerConfiguration _configuration;
19
20         /// <summary>
21         /// 构造函数
22         /// </summary>
23         /// <param name="config"></param>
24         public IdefavAuthorizationServerHost(AuthorizationServerConfiguration config)
25         {
26             if (config != null)
27                 _configuration = config;
28         }
29
30         /// <summary>
31         /// Token创建
32         /// </summary>
33         /// <param name="accessTokenRequestMessage"></param>
34         /// <returns></returns>
35         public AccessTokenResult CreateAccessToken(IAccessTokenRequest accessTokenRequestMessage)
36         {
37             var accessToken = new AuthorizationServerAccessToken();
38             accessToken.Lifetime = _configuration.TokenLifetime;//设置Token的有效时间
39
40             // 设置加密公钥
41             accessToken.ResourceServerEncryptionKey =
42                 (RSACryptoServiceProvider)_configuration.EncryptionCertificate.PublicKey.Key;
43             // 设置签名私钥
44             accessToken.AccessTokenSigningKey = (RSACryptoServiceProvider)_configuration.SigningCertificate.PrivateKey;
45
46             var result = new AccessTokenResult(accessToken);
47             return result;
48         }
49
50         public IClientDescription GetClient(string clientIdentifier)
51         {
52             // 这里需要去验证客户端发送过来的client_id
53             if (string.Equals(clientIdentifier, "idefav", StringComparison.CurrentCulture))// 这里为了简明起见没有使用数据库
54             {
55                 var client=new Client
56                 {
57                     Name = "idefav",
58                     ClientSecret = "1",
59                     ClientType = 1
60                 };
61                 return client;
62             }
63             throw new ArgumentOutOfRangeException("clientIdentifier");
64         }
65
66         public bool IsAuthorizationValid(IAuthorizationDescription authorization)
67         {
68             return true;
69         }
70
71         public AutomatedUserAuthorizationCheckResponse CheckAuthorizeResourceOwnerCredentialGrant(string userName, string password,
72             IAccessTokenRequest accessRequest)
73         {
74             throw new NotImplementedException();
75         }
76
77         public AutomatedAuthorizationCheckResponse CheckAuthorizeClientCredentialsGrant(IAccessTokenRequest accessRequest)
78         {
79             AutomatedUserAuthorizationCheckResponse response = new AutomatedUserAuthorizationCheckResponse(accessRequest, true, "test");
80             return response;
81         }
82
83         public ICryptoKeyStore CryptoKeyStore { get; }
84         public INonceStore NonceStore { get; }
85
86
87     }
88 }

4、实现OAuthController

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using System.Web;
 6 using System.Web.Mvc;
 7 using DotNetOpenAuth.Messaging;
 8 using DotNetOpenAuth.OAuth2;
 9 using IdefavAuthorizationServer.Code;
10
11 namespace IdefavAuthorizationServer.Controllers
12 {
13     public class OAuthController : Controller
14     {
15         private readonly AuthorizationServer authorizationServer =
16            new AuthorizationServer(new IdefavAuthorizationServerHost(Common.Configuration));
17
18         public async Task<ActionResult> Token()
19         {
20             var response = await authorizationServer.HandleTokenRequestAsync(Request);
21             return response.AsActionResult();
22         }
23     }
24 }

5、初始化AuthorizationServerConfiguration

这里采用Windows签名证书

放到项目中

制作证书事注意:要加上-a sha1  -sky exchange

到此,基本代码就写完了,现在说说要注意的地方,OAuth2默认设置的请求是要求SSL的也就是必须是https//localhost:1111/OAuth/Token,然后我们现在不需要使用SSL加密请求,更改一下WebConfig文件

在WebConfig里面设置成如图中那样,就可以不用https访问了

6、我们F5运行项目

使用Post工具发送Post请求访问 http://localhost:53022/OAuth/token

Body参数:

1 client_id:idefav
2 client_secret:1
3 grant_type:client_credentials

请求结果:

这样我们就拿到了access_token,通过这个access_token我们就可以访问资源服务器了

更新:

OAuthController代码添加内容类型

 1 using System.Collections.Generic;
 2 using System.Linq;
 3 using System.Threading.Tasks;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Script.Services;
 7 using DotNetOpenAuth.Messaging;
 8 using DotNetOpenAuth.OAuth2;
 9 using IdefavAuthorizationServer.Code;
10
11 namespace IdefavAuthorizationServer.Controllers
12 {
13     public class OAuthController : Controller
14     {
15         private readonly AuthorizationServer authorizationServer =
16            new AuthorizationServer(new IdefavAuthorizationServerHost(Common.Configuration));
17
18         public async Task<ActionResult> Token()
19         {
20             var response = await authorizationServer.HandleTokenRequestAsync(Request);
21             Response.ContentType = response.Content.Headers.ContentType.ToString();
22             return response.AsActionResult();
23         }
24     }
25 }

鉴于有人不知道Windows签名制作,下篇我们一起来看看如何制作一个认证服务器可以使用的签名证书

转载于:https://www.cnblogs.com/idefav2010/p/DotNetOpenAuth.html

DotNetOpenAuth实践之搭建验证服务器相关推荐

  1. 阿里云服务器开发实践:使用云服务器ECS搭建DoH服务

    我们都知道,DNS服务器(Domain Name Server)可以将人类易于理解的域名(就是平时说的网址)转换为机器易于理解的「IP 地址」,它使用UDP端口53对外提供服务.通常,DNS查询是明文 ...

  2. AAA验证和ciscorescue v4.2 验证服务器的搭建(telnet方式和级别的设置)

    AAA验证服务器的工作原理: AAA是Authentication,Authorization and Accounting(认证.授权和计费)的简 称,它提供了一个对认证.授权和计费这三种安全功能进 ...

  3. iis搭建ftp服务器及身份验证设置

    在win7下搭建ftp服务器: 搭建FTP安装步骤: 1.安装FTP服务器: 1)点击[开始]-[控制面板]-[程序]-[程序和功能]-[打开或关闭windows功能] 图一:安装FTP服务器(1) ...

  4. WampServer 搭建本地服务器及 XSS 基本原理和初步实践(一)

    目录 一.WampServer 搭建本地服务器 1.WampServer 下载安装及变小绿标 方法一:官网 方法二:中文站 问题及解决 2.访问本地服务器相关配置 1.检查是否正常搭建服务器 2.关闭 ...

  5. Ubuntu虚拟机上搭建PPPoE服务器并进行本地验证

    Ubuntu虚拟机上搭建PPPoE服务器并进行本地验证 本地环境 环境说明 实验拓扑 环境说明 环境搭建 对PPPoE服务器进行配置并验证 全局配置 关于PPPoE Server的配置 添加测试用的账 ...

  6. 信安实践——自建CA证书搭建https服务器

    https://www.cnblogs.com/libaoquan/p/7965873.html 1.理论知识 https简介 HTTPS(全称:Hyper Text Transfer Protoco ...

  7. 信息安全实践Lab1-自建CA证书搭建https服务器

    title: 信息安全实践Lab1-自建CA证书搭建https服务器 date: 2021-12-21 02:44:40 tags: 信息安全 categories: 信息安全实践 信息安全实践Lab ...

  8. esp虚拟服务器,esp32搭建web服务器

    esp32搭建web服务器 内容精选 换一换 云解析服务支持为域名快速添加网站解析,通过该功能可以简化解析记录的配置,包含如下两种场景:网站解析至IP地址:为域名的主域名和子域名分别添加一条A类型记录 ...

  9. 【验】Postfix+Dovecot+MySQL搭建邮件服务器

    网上有很多使用Postfix搭建邮件服务器的文章,但目前貌似没有看到较为完整的一篇.本例将尝试在Ubuntu系统中使用Postfix+Dovecot+MySQL搭建邮件服务器. 说到邮件服务器,网上有 ...

最新文章

  1. [Windows][C#][.NET][WPF]基于ArcFace2.0+红外双目摄像头的活体检测
  2. Orchid兰花协议简介——分布式匿名代理网络
  3. java 直播服务器_MyLive--使用JAVA实现的直播RTMP服务器
  4. gpt windows linux,UEFI+GPT环境下安装Windows+Linux+OS X
  5. 用栈实现算术表达式 java_java的栈和用栈来解析算术表达式
  6. java 多线程共享变量两类问题_Java并发基础09. 多个线程间共享数据问题
  7. 实现用于意图识别的文本分类神经网络
  8. 【html】非配对的标签
  9. 职场 软件实施工程师的地位以及发展前景
  10. 2020年苏大计算机考研872真题及解析
  11. SSL安全证书:免费的SSL证书申请渠道有哪些?
  12. Arduino UNO步进电机控制
  13. stm32f103r8t6的晶振频率_STM32F103R8T6 中文资料
  14. 多余的回车键(Enter)
  15. SICP习题2.6 题目理解
  16. 马云:我肯定是12x12,你们却在纠结不能996
  17. c语言while输出26个字母,菜鸟求助,写一个随机输出26个英文字母的程序
  18. uniapp用canvas实现分享海报
  19. 关于PHP工程师职业资格证书的了解
  20. C++11新特性(原封不动转载待查)

热门文章

  1. python是什么牌子主机_python 收集主机信息
  2. matlab读txt文件不完整,求助Matlab批量读取TXT文件出错
  3. js 转化为实体符_js转html实体的方法
  4. 光纤收发器在高清网络视频监控工程项目中的应用
  5. 开关量光端机产品特点及应用范围介绍
  6. linux执行脚本n,Linux执行sh脚本空白
  7. Github常用搜索指令(毕设资料搜索必备)
  8. 武汉科技大学计算机研究生拟录取名单,武汉科技大学2021年硕士研究生拟录取名单公示...
  9. martingale、markov chain、Monte Carlo、MCMC
  10. php如何禁用浏览器的缓存,php如何禁止浏览器使用缓存页面