实习在学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数据的更多相关文章

  1. SSO单点登录在web上的关键点 cookie跨域

    概述 其实WEB单点登录的原理挺简单的,抛开那些复杂的概念,简单来讲讲如何实现一个最基本的单点登录 首先需要有两个程序 例如:http://www.site-a.com 我们简称A http://ww ...

  2. 2017-2018-2 20155303『网络对抗技术』Final:Web渗透获取WebShell权限

    2017-2018-2 『网络对抗技术』Final:Web渗透获取WebShell权限 --------CONTENTS-------- 一.Webshell原理 1.什么是WebShell 2.We ...

  3. web项目获取资源文件

    首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 专题 招聘 ITeye GitChat GitChat 图文课 写博客 消息 1 评论 关注 点赞 回答 系 ...

  4. 【转】Java Web 项目获取运行时路径 classpath

    Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...

  5. web qq 获取好友列表hash算法

    web qq 获取好友列表hash算法 在使用web qq的接口进行好友列表获取的时候,需要post一个参数:hash 在对其js文件进行分析之后,发现计算hash的函数位于: http://0.we ...

  6. jQuery Ajax使用FormData上传文件和其他数据,后端web.py获取

    参考博文: 通过jQuery Ajax使用FormData对象上传文件 方法一:使用<form>表单初始化FormData对象方式上传文件 前端(JQuery): <form enc ...

  7. 微信小程序API 登录-wx.login(OBJECT) + 获取微信用户唯一标识openid | 小程序

    wx.login(OBJECT) 调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key).用户数据的加解密通讯需要 ...

  8. Python爬虫实战五之模拟登录淘宝并获取所有订单

    经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 温馨提示 更新时间,2016-02-01,现在淘宝换成了滑块验证了 ...

  9. HTTP协议下保证登录密码不被获取最健壮方式

    原文:http://www.cnblogs.com/intsmaze/p/6009648.html HTTP协议下保证登录密码不被获取最健壮方式   说到在http协议下用户登录如何保证密码安全这个问 ...

随机推荐

  1. Windows快捷安装应用方法(此处以Virtualbox为例)

    1.执行已下载的virtualbox的安装exe文件,使用pywinauto模拟点击Windows安装的对应控件 1.1.启动exe文件 start *.exe 1.2.使用pywinauto(也适用 ...

  2. Docker非root用户使用

    Docker 用户管理 安装Docker后docker相关命令都需要加上sudo才能执行,这里为特定用户添加下权限 Docker群组 不过一般安好docker后该群组已创建 sudo groupadd ...

  3. html的基础01

    1.什么是网页 2.常用的浏览器有哪些 3.web标准是什么  1.什么是网页  2.常用的浏览器 360.百度那些都是国产浏览器,内核一样,以上六个都是国际浏览器,不同厂商生产(但IE和Edge都是 ...

  4. vue-resource && axios

    1 # axios 2 # 1.安装:npm i axios 3 # 2.使用: 4 import axios from 'axios' 5 axios.get(URL).then(response= ...

  5. C#反射跟特性

    一.什么是反射? 了解反射之前我们必须知道一个概念--元数据.有关程序和程序类型的信息叫做元数据,通俗的解释就是类里面的方法.属性.字段等. 而程序在运行的时候去查看其它程序集的行为就叫做反射.在我们 ...

  6. while 循环、do- while 循环 和 for 循环之间的那点事

    C语言自学之三种循环比较 使用循环计算1-2+3-4+5-6+--100的值?    在编辑器中给出了三种循环体结构的部分代码,请选择合适的循环结构补全代码实现此功能.    运行结果为: sum=- ...

  7. Spring源码 04 IOC XML方式

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  8. Canvas 非常重要的三个函数

    beginPath 绘制路径必须添加 beginPath().它标志着一个画笔在画布中哪个地方开始画起.没有它,新起的画笔位置必定与上一次画笔结束的位置相连. // 第一个半圆 ctx.arc(60, ...

  9. MySQL查询关键数据方法

    MySQL查询关键数据方法 操作表的SQL语句补充 1.修改表名 alter table 表名 reame 新表名: 2.新增字段名 alter table 表名 add 字段名 字段类型(数字) 约 ...

  10. R型医用变压器为什么越来越受大众喜爱?

    传统的家用电器.手机行业在2018年给电子变压器领域产生重要的冲击性,现如今,智能医疗领域日渐增加,正好是R型医疗变压器行业转型的突破口. 近些年,在我国医疗器械领域因为一个新的科技进步和工程设计持续 ...