OAuth2认证和授权:AuthorizationCode认证
前面的OAuth2认证,里面的授权服务器都是用的identityserver4搭建的
ids4没有之前一般都是Owin搭建授权服务器,博客园有很多
ids4出来后,一般都是用ids4来做认证和授权了,
所以这里简单说下AuthorizationCode认证,但授权服务器依然是ids4
下篇接受ids4的认证和授权
ConfigureServices配置:
#region OAuth认证
services.AddAuthentication(options =>
{
//options.DefaultAuthenticateScheme=OAuthDefaults.DisplayName
//options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = "Cookies";
options.DefaultSignInScheme = "Cookies";
//options.DefaultSignOutScheme = "Cookies";
options.DefaultChallengeScheme = "OAuth";
})
.AddCookie()
.AddOAuth("OAuth", options =>
{
options.ClientId = "OAuth.Client";
options.ClientSecret = "secret";
options.AuthorizationEndpoint = "http://localhost:5003/connect/authorize";
options.TokenEndpoint = "http://localhost:5003/connect/token";
options.CallbackPath = new PathString("/OAuth");
options.SaveTokens = true;
options.Scope.Add("OAuth1");
options.Scope.Add("OAuth2");
options.Scope.Add("OAuth3");
//options.Scope.Add("offline_access");
options.Events = new OAuthEvents()
{
//OnRedirectToAuthorizationEndpoint = t =>
//{
// t.Response.Redirect("http://localhost:5001/Account/userinfo");
// return Task.FromResult(0);
//}, //远程异常触发
OnRemoteFailure = OAuthFailureHandler =>
{
//var msg = OAuthFailureHandler.Failure.Message;
var authProperties = options.StateDataFormat.Unprotect(OAuthFailureHandler.Request.Query["state"]);
var redirectUrl = authProperties.RedirectUri;
if (redirectUrl.Contains("/"))
{
redirectUrl = string.Format($"{redirectUrl.Substring(0, redirectUrl.LastIndexOf("/") + 1)}#"); // redirectUrl.Substring(0, redirectUrl.IndexOf("/") + 1);
}
//"http://localhost:5001/#"
OAuthFailureHandler.Response.Redirect(redirectUrl);
OAuthFailureHandler.HandleResponse();
return Task.FromResult();
}
}; });
#endregion
中间件:
app.UseAuthentication();
授权服务器的ApiResource配置
var oauth = new ApiResource
{
Name = "OAuth.ApiName", //这是资源名称
Description = "",
DisplayName = "",
Scopes = {
new Scope{
Name="OAuth1", //这里是指定客户端能使用的范围名称 , 是唯一的
Description="描述",
DisplayName="获得你的个人信息,好友关系",
Emphasize=true,
Required=true,
//ShowInDiscoveryDocument=true,
},
new Scope{
Name="OAuth2",
Description="描述",
DisplayName="分享内容到你的博客",
Emphasize=true,
Required=true,
},
new Scope{
Name="OAuth3",
Description="描述",
DisplayName="获得你的评论",
}
}
};
当选择使用微博登陆。就会跳转到授权服务器,使用微博账号登陆

当然,如果你取消,则会跳转回来,是根据OnRemoteFailure事件来的

登陆成功后,则提示是否同意授权

如果取消,则也会跳回之前的页面
同意授权后,则跳转回来,拿到了access_token ,可以请求资源服务器获取资源了
从5003跳转到了5001

就这么一个简单的过程,下篇详细接受下ids4,感觉那才是重点
OAuth2认证和授权:AuthorizationCode认证的更多相关文章
- keycloak~账号密码认证和授权码认证
用户名密码登录 POST /auth/realms/demo/protocol/openid-connect/token 请求体 x-www-form-urlencoded grant_type:pa ...
- ASP.NET Core WebAPI中使用JWT Bearer认证和授权
目录 为什么是 JWT Bearer 什么是 JWT JWT 的优缺点 在 WebAPI 中使用 JWT 认证 刷新 Token 使用授权 简单授权 基于固定角色的授权 基于策略的授权 自定义策略授权 ...
- OAuth2.0认证和授权原理
什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...
- [转载] OAuth2.0认证和授权原理
转载自http://www.tuicool.com/articles/qqeuE3 什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准,允许第三方网站在用户授权的前 ...
- 一步步搭建最简单oauth2.0认证和授权
oauth2.0 最早接触这个概念是在做微信订阅号开发.当时还被深深的绕进去,关于oauth2.0的解释网上有好多,而且都讲解的比较详细,下面给大家价格参考资料. http://owin.org/ h ...
- OAuth2.0认证和授权以及单点登录
https://www.cnblogs.com/shizhiyi/p/7754721.html OAuth2.0认证和授权机制讲解 2017-10-30 15:33 by shizhiyi, 2273 ...
- OAuth2认证和授权:ResourceOwnerPassword认证
ResourceOwnerPassword在 ClientCredentials认证上新增了用户名和密码 但通过RequestPasswordTokenAsync获取不到refresh_token,不 ...
- OAuth2认证和授权:ClientCredentials认证
1:创建授权服务器项目:AuthorizationServer,添加包:IdentityServer4 2:创建资源服务器项目:ResourcesServer,添加包:IdentityServer4. ...
- OAuth2认证和授权入门
OAuth2四种授权方式 四种授权方式 OAuth 2.0定义了四种授权方式. 密码模式(resource owner password credentials) 授权码模式(authorizatio ...
随机推荐
- comake2
http://blog.csdn.net/lsjseu/article/details/23395565 comake允许用户通过编写COMAKE文件,来帮助用户管理编译依赖以及编译环境的开发工具: ...
- go语言fallthrough的用法心得
fallthrough:Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面 ...
- chrome 下 input[file] 元素cursor设置pointer不生效的解决
https://jingyan.baidu.com/article/48b558e32fabb67f38c09a81.html 环境是chrome浏览器,今天发现为html网页中的input [fil ...
- VS调试不能进入断点,提示当前不会命中断点还未为文档加载任何符号
经过仔细检查后发现,是DLL版本和源码生成的DLL版本不一致,造成的! 复制新的过去,问题就解决了.
- 承上 DBlink 与 SCN | 新增视图找出外部 SCN 跳变
综述 SQL> set pages 100 lines 200 col result for a15 col OPERATION_TIMESTAMP for a35 col HOST_N ...
- tensorflow c/c++库使用方法
tensorflow目前支持最好的语言还是python,但大部分服务都用C++ or Java开发,一般采用动态链接库(.so)方式调用算法,因此tensorflow的c/c++ API还是有必要熟悉 ...
- echarts网络拓扑图
option = { title: { text: '' }, tooltip: {}, animationDurationUpdate: 1500, animationEasingUpdate: ' ...
- Win10 高分屏软件界面字体模糊问题解决
只需要将自定义缩放值改为 124% 即可
- TensorFlow学习入门
学习了基本的神经网络知识后,要使用框架了,这样才能出来更加复杂的情况,更快的开发出模型. 首先安装后,按照官网写了一个例子,但是又好多不懂,但只是第一步, 看这段代码,其实给你提供了很多学习tf的线索 ...
- 微信小程序本地的域名“不在以下request合法域名列表中”错误处理方法