C#实现登录某web进而获取其token数据
实习在学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;
}
}
}
}
C#实现登录某web进而获取其token数据的更多相关文章
- SSO单点登录在web上的关键点 cookie跨域
概述 其实WEB单点登录的原理挺简单的,抛开那些复杂的概念,简单来讲讲如何实现一个最基本的单点登录 首先需要有两个程序 例如:http://www.site-a.com 我们简称A http://ww ...
- 2017-2018-2 20155303『网络对抗技术』Final:Web渗透获取WebShell权限
2017-2018-2 『网络对抗技术』Final:Web渗透获取WebShell权限 --------CONTENTS-------- 一.Webshell原理 1.什么是WebShell 2.We ...
- web项目获取资源文件
首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 专题 招聘 ITeye GitChat GitChat 图文课 写博客 消息 1 评论 关注 点赞 回答 系 ...
- 【转】Java Web 项目获取运行时路径 classpath
Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...
- web qq 获取好友列表hash算法
web qq 获取好友列表hash算法 在使用web qq的接口进行好友列表获取的时候,需要post一个参数:hash 在对其js文件进行分析之后,发现计算hash的函数位于: http://0.we ...
- jQuery Ajax使用FormData上传文件和其他数据,后端web.py获取
参考博文: 通过jQuery Ajax使用FormData对象上传文件 方法一:使用<form>表单初始化FormData对象方式上传文件 前端(JQuery): <form enc ...
- 微信小程序API 登录-wx.login(OBJECT) + 获取微信用户唯一标识openid | 小程序
wx.login(OBJECT) 调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key).用户数据的加解密通讯需要 ...
- Python爬虫实战五之模拟登录淘宝并获取所有订单
经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 温馨提示 更新时间,2016-02-01,现在淘宝换成了滑块验证了 ...
- HTTP协议下保证登录密码不被获取最健壮方式
原文:http://www.cnblogs.com/intsmaze/p/6009648.html HTTP协议下保证登录密码不被获取最健壮方式 说到在http协议下用户登录如何保证密码安全这个问 ...
随机推荐
- OpenWrt之feeds.conf.default详解
目录 OpenWrt之feeds.conf.default详解 文件内容 命令解释 src-svn与src-gitsvn src-git与src-git-full src-cpy与src-link 其 ...
- Border Theory
持续更新中!!!更个屁,无线停更! 前言: KMP 学傻了,看 skyh 说啥 border 树,跑来学 border 理论 洛谷云剪切板:https://www.luogu.com.cn/paste ...
- 转一篇MYSQL文章《数据库表设计,没有最好只有最适合》
http://mp.weixin.qq.com/s/a8klpzM5iam0_JYSw7-U4g 我们在设计数据库的时候,是否会突破常规,找到最适合自己需求的设计方案,下面来举个例子: 常用的邻接表设 ...
- 正则表达式实战:最新豆瓣top250爬虫超详细教程
检查网页源代码 首先让我们来检查豆瓣top250的源代码,一切网页爬虫都需要从这里开始.F12打开开发者模式,在元素(element)页面通过Ctrl+F直接搜索你想要爬取的内容,然后就可以开始编写正 ...
- ceph 005 赋权补充 rbd块映射
我的ceph版本 [root@serverc ~]# ceph -v ceph version 16.2.0-117.el8cp (0e34bb74700060ebfaa22d99b7d2cdc037 ...
- One---python的六种数据类型及数据转换
python的六种数据类型 python中数据类型分为不可变数据类型和可变数据类型 可变数据类型 可变数据类型包括:List(列表).Dictionary(字典).Set(集合) 不可变数据类型 不可 ...
- LuoguU72177 火星人plus (逆康拓展开)
没开long long见祖宗... BIT先求逆序对来造表存展开关系,线段树维护01进制 #include <iostream> #include <cstdio> #incl ...
- Git 08 IDEA撤销添加
参考源 https://www.bilibili.com/video/BV1FE411P7B3?spm_id_from=333.999.0.0 版本 本文章基于 Git 2.35.1.2 如果将不想添 ...
- linux centos 系统盘文件系统损坏-已解决
当我们使用的Linux虚拟机(云服务器/vps)磁盘出现xfs文件系统损坏时,该如何进行修复? xfs格式文件系统损坏,是运维常见的一个场景,经常发生在强制重启.异常关机.软件冲突.误删文件等事件后, ...
- 2019 CSP-S Ⅱ 游记
day0(试机) 第零天,重新打了一遍头文件和读优,熟悉了一下就匆匆走了. day1 T1一看到先把二分打了,然后发现long long要爆,好慌 主要是基础知识不够扎实,不知道unsigned lo ...