实习在学C#,记录一下学习过程!

首先是需求描述(基于C#的.net core MVC实现):

User:
Resource Owner

Agent:
Brower

auth.brightspace.com:
Authorization Server

politemalltest.brightspace.com:
Resource Server

1. Web Page:
  1.1 host must be https://localhost:3434
2. Button:
  2.1 onclick <a href="/d2lauth">
  2.2 get https://localhost:3434/d2lauth
  2.3 response 302 https://auth.brightspace.com/oauth2/auth?response_type=code&client_id=*******&state=ssss&scope=core%3A*%3A*&redirect_uri=https%3A%2F%2Flocalhost%3A3434%2Fredirecturi

3. Api:
  3.1 build api for POST https://localhost:3434/redirecturi to received Auth Code
  3.2 Use Auth code to get Token from [POST https://auth.brightspace.com/core/connect/token] (use HttpClient)
  3.3 Save Token to database

URL:
https://politemalltest.brightspace.com/d2l/login?noredirect=1
Username:***********
Password: ************

首先将本地服务器端口号设置为3434,具体操作为,找到lunchsettings文件,修改url

然后我们要向https://localhost:3434/d2lauth发送get请求,然后跳转到 https://auth.brightspace.com/oauth2/auth?response_type=code&client_id=0055e1d6-0d67-47b3-9169-                                                  b329a4af7eae&state=ssss&scope=core%3A*%3A*&redirect_uri=https%3A%2F%2Flocalhost%3A3434%2Fredirecturi

然后我们要向 https://localhost:3434/redirecturi发送post请求,来获取鉴权码,也就是authcode,因为我们是基于Oauth2.0协议的。要获取token,首先服务端会验证客户端身份,验证成功后,会返给一个authcode,然后客户端拿着authcode,去换取token,进而在服务端获取相关资源!

接下来就是主要获取token的操作,我们在controller下的文件,编写一个控制器,发送请求,代码如下:

             string authcode = this.HttpContext.Request.Query["code"];

            HttpClient client = new HttpClient();

            Dictionary<string, string> headers = new Dictionary<string, string>();

            headers["grant_type"] = "authorization_code";
//headers["grant_type"] = "refresh_token";
headers["client_id"] = "*************";
headers["client_secret"] = "********************888";
headers["code"] = authcode;
//headers["refresh_token"] = "rt.ap-southeast-1.z9I0S2XTQ9EMCRUVaAil8_4C83X7X8yt_6ExSF6VRyk";
headers["redirect_uri"] = "https://localhost:3434/redirecturi"; FormUrlEncodedContent content = new FormUrlEncodedContent(headers); var url = "https:****************/connect/token"; var response = await client.PostAsync(url, content); string result = response.Content.ReadAsStringAsync().Result;
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(result); string access_token = values["access_token"];

代码结构很清晰,逻辑就是获取重定向后的authcode,然后我们使用httpclient实例话一个对象,用来发送请求,然后创建一个字典结构,用来存放请求头信息,获取token的必要参数如代码所示,client_id,client_sercet,grant_type,redirect_uri,code,然后我们对这些内容进行编码。之后的操作也很容易理解,response表示发送请求之后的回应,result表示获取到的结果,然后反序列化内容,进而等到token数据中的access_token.

如图,我们就获取了token中的access_token数据了!

上述代码表达了获取token的大致思路,下面看一下结构化代码:

using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Headers;
using System.Text; namespace TokenRequester.Controllers
{
public class Token_Get : Controller {
public class AccessTokenSend
{
public string client_id { get; set; }
public string client_secret { get; set; }
public string code { get; set; }
public string grant_type { get; set; }
public string redirect_url { get; set; }
}
public IActionResult Index()
{
return Redirect("https://auth.brightspace.com/oauth2/auth?response_type=code&client_id=0055e1d6-0d67-47b3-9169-b329a4af7eae&state=ssss&scope=core%3A*%3A*&redirect_uri=https%3A%2F%2Flocalhost%3A3434%2Fredirecturi");
}
public async Task<string> GetAccess()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://auth.brightspace.com/core/connect/token");
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType); AccessTokenSend accessTokenSend = new AccessTokenSend()
{
client_id = "_client_id_",
client_secret = "_client_secret",
code = "_authorisation_code_from_url",
grant_type = "authorization_code",
redirect_url = "_redirect_url"
};
var json = Newtonsoft.Json.JsonConvert.SerializeObject(accessTokenSend);
var data = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/x-www-from-urlencoded"); var result = await client.PostAsync("https://auth.brightspace.com/core/connect/token", data);
string resultContent = await result.Content.ReadAsStringAsync(); return resultContent;
}
}
}
}

最新文章

  1. Gevent中的同步与异步详解
  2. sql server 2000通过机器名可以连,通过ip连不上的问题
  3. uva 514
  4. shell编程基础(3)条件判断语句
  5. ios ReactiveViewModel
  6. XAMPP配置虚拟主机
  7. Linux2.6内核--内存管理(2)--区
  8. JY03-HTML/CSS-京东02
  9. 2015年网易考拉海淘android面试
  10. 用C语言怎么实现复制自己
  11. 关于wxFileSystemWatcher输出文件名的解决方法
  12. 非正确使用浮点数据由项目产生BUG讨论的问题
  13. 浙大pat1050题解
  14. shell 提取字符串
  15. java线程(二)
  16. Object Detection / Human Action Recognition 项目
  17. operator用法:隐式类型转换
  18. DNS搭建
  19. js获取当前页面的url网址信息小汇总
  20. Java lombok插件介绍

热门文章

  1. C#基础-面向对象详解
  2. std::hash&lt;std::pair&lt;int, int&gt; &gt;
  3. 基于Docker-compose搭建Redis高可用集群-哨兵模式(Redis-Sentinel)
  4. 协程 &amp;&amp; 异步例子
  5. Qt 国际化翻译
  6. DolphinScheduler 在绿瘦的实践成果,开启了我的开源之路!
  7. java中list集合的几种去重方式
  8. AI 音辨世界:艺术小白的我,靠这个AI模型,速识音乐流派选择音乐 ⛵
  9. Docker 安装 MySQL、Redis
  10. C++工厂方法模式讲解和代码示例