asp.net权限认证:OWIN实现OAuth 2.0 之简化模式(Implicit)
简化模式定义
通过客户端的后台服务器,与“服务提供商”的认证服务器进行认证。(和授权码模式差不多哦)
1、用户访问客户端,后者将前者导向认证服务器。
2、用户选择是否给予客户端授权。
3、假设用户给予授权,认证服务器会直接向客户端发送访问令牌(access token)。
4、Client拿着access token去访问Resource资源
注意:红色字体部分是与授权码模式最根本的区别哦
简化模式的工作流程图:

图 1 (网上搜到的简化模式工作流程图说明)
新建项目:ImplicitGrant
AuthorizationServer与ResourceServer还是用之前的项目

新建Index.cshtml
<form id="form1">
<div>
Access Token<br />
<input id="AccessToken" name="AccessToken" />
<input id="Authorize" type="button" name="signin.AccessToken" value="向认证服务器申请授权" />
<br />
<input id="CallApi" name="submit.CallApi" value="访问受控资源" type="button" />
</div>
<div id="output">
</div>
</form>
var authorizeUri = 'http://localhost:8270/OAuth/Authorize';
var returnUri = 'http://localhost:3622/Home/SignIn';
var apiUri = 'http://localhost:8001/api/Values'; $('#Authorize').click(function () {
var nonce = 'my-nonce'; var uri = addQueryString(authorizeUri, {
'client_id': '7890ab',
'redirect_uri': returnUri,
'state': nonce,
'scope': 'scope1 scope2',
'response_type': 'token',
}); window.oauth = {};
window.oauth.signin = function (data) {
if (data.state !== nonce) {
return;
} $('#AccessToken').val(data.access_token);
} window.open(uri, 'Authorize', 'width=640,height=480');
}); $('#CallApi').click(function () {
$.ajax(apiUri, {
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + $('#AccessToken').val());
},
dataType: 'text',
cache: false,
success: function (data) {
console.log(data);
$('#output').text(data);
}
});
});
OK,自此,简化模式测试项目有效代码已经完成了
注意:逻辑是故意写在html页面而没有写在后台cs页面的哦,这是简化模式形成的原因
运行项目试试
开始授权
点击认证按钮,出现认证页面和授权码页面一样

点击授权,直接返回的就是token

点击访问受控资源,发现没有预期那样返回资源数据

这是js跨域的问题,需要在ResourceServer项目加上“microsoft.aspnet.webapi.cors”引用
并在WebApiConfig.cs页面加上
// 跨域配置
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
在ValuesController中的Get方式上贴上[HttpGet]
public class ValuesController : ApiController
{
[HttpGet]
[Authorize]
public string Get()
{
return "lanxiaoke";
}
}
再次试试,成功返回数据

asp.net权限认证系列
- asp.net权限认证:Forms认证
- asp.net权限认证:HTTP基本认证(http basic)
- asp.net权限认证:Windows认证
- asp.net权限认证:摘要认证(digest authentication)
- asp.net权限认证:OWIN实现OAuth 2.0 之客户端模式(Client Credential)
- asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)
- asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)
- asp.net权限认证:OWIN实现OAuth 2.0 之简化模式(Implicit)
asp.net权限认证:OWIN实现OAuth 2.0 之简化模式(Implicit)的更多相关文章
- asp.net权限认证:OWIN实现OAuth 2.0 之客户端模式(Client Credential)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net权限认证:Windows认证
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net权限认证:Forms认证
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net权限认证:HTTP基本认证(http basic)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net权限认证:摘要认证(digest authentication)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- [转]asp.net权限认证:摘要认证(digest authentication)
本文转自:http://www.cnblogs.com/lanxiaoke/p/6357501.html 摘要认证简单介绍 摘要认证是对基本认证的改进,即是用摘要代替账户密码,从而防止明文传输中账户密 ...
- [转]asp.net权限认证:HTTP基本认证(http basic)
本文转自:http://www.cnblogs.com/lanxiaoke/p/6353955.html HTTP基本认证示意图 HTTP基本认证,即http basic认证. 客户端向服务端发送一个 ...
随机推荐
- /etc/profile文件
导读:Linux /etc/profile文件的改变会涉及到系统的环境,也就是有关Linux环境变量的东西,学习Linux要了解Linux profile文件的相关原理,这里对则以文件进行具体分析.这 ...
- laravel安装excel功能
原文安装链接:https://github.com/Maatwebsite/Laravel-Excel 代码如下: if ($rows = DB::connection('glist')->ta ...
- eclipse注释模板设置(未整理)
Window --> Java --> Code Style --> Code Templates --> Comments --> types --> Edit ...
- linux命令学习-1-less
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- iOS开发—— UIImagePickerController获取相册和拍照
一.简单的拍照显示,或是从相册中直接选取照片 #import "ViewController.h" @interface ViewController ()<UIImageP ...
- literal
literal[英][ˈlɪtərəl][美][ˈlɪtərəl]adj.照字面的; 原义的; 逐字的; 平实的,避免夸张; n.[印]错排,文字上的错误;
- CentOS下架设Telnet服务器
CentOS下架设Telnet服务器1.什么是Telnet?来自度娘的解释:Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机 ...
- Grunt实现自动化单元测试
http://www.tuicool.com/articles/rAnaYvn 直奔主题: 一.安装grunt-contrib-qunit npm install grunt-contrib-qu ...
- BUG--tomcat更改目录失败
1.问题描述 按照通用的做法,tomcat可以配置运行的网站目录.但是配置后不能正确运行. 配置过程如:http://www.cnblogs.com/SimonGao/p/4905887.html 2 ...
- 只能输入数字的文本框-php
导读:<inputtype="text"name="textfield"onKeyPress="javascript:alert(event k ...