前面的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认证的更多相关文章

  1. keycloak~账号密码认证和授权码认证

    用户名密码登录 POST /auth/realms/demo/protocol/openid-connect/token 请求体 x-www-form-urlencoded grant_type:pa ...

  2. ASP.NET Core WebAPI中使用JWT Bearer认证和授权

    目录 为什么是 JWT Bearer 什么是 JWT JWT 的优缺点 在 WebAPI 中使用 JWT 认证 刷新 Token 使用授权 简单授权 基于固定角色的授权 基于策略的授权 自定义策略授权 ...

  3. OAuth2.0认证和授权原理

    什么是OAuth授权?   一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...

  4. [转载] OAuth2.0认证和授权原理

    转载自http://www.tuicool.com/articles/qqeuE3 什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准,允许第三方网站在用户授权的前 ...

  5. 一步步搭建最简单oauth2.0认证和授权

    oauth2.0 最早接触这个概念是在做微信订阅号开发.当时还被深深的绕进去,关于oauth2.0的解释网上有好多,而且都讲解的比较详细,下面给大家价格参考资料. http://owin.org/ h ...

  6. OAuth2.0认证和授权以及单点登录

    https://www.cnblogs.com/shizhiyi/p/7754721.html OAuth2.0认证和授权机制讲解 2017-10-30 15:33 by shizhiyi, 2273 ...

  7. OAuth2认证和授权:ResourceOwnerPassword认证

    ResourceOwnerPassword在 ClientCredentials认证上新增了用户名和密码 但通过RequestPasswordTokenAsync获取不到refresh_token,不 ...

  8. OAuth2认证和授权:ClientCredentials认证

    1:创建授权服务器项目:AuthorizationServer,添加包:IdentityServer4 2:创建资源服务器项目:ResourcesServer,添加包:IdentityServer4. ...

  9. OAuth2认证和授权入门

    OAuth2四种授权方式 四种授权方式 OAuth 2.0定义了四种授权方式. 密码模式(resource owner password credentials) 授权码模式(authorizatio ...

随机推荐

  1. re.S、 re.M

    re.S是代表.可以匹配\n以及“  re.M是多行   code import re a = '''asdfsafhellopass: 234455 worldafdsf ''' b = re.fi ...

  2. .net Core Abp See config settings - "CustomSchemaIds" for a workaround

    Swagger  See config settings - "CustomSchemaIds" for a workaround System.InvalidOperationE ...

  3. Hlacon 之Image ,Region,XLD

    一 读取的3种方式: read_image( image,'filename') //image 是输出对象,后面是输入文件的路径和名称 读取多图: 1,申明一个数组,分别保存路径 ImagePath ...

  4. 12 Best Live Chat Software for Small Business Compared (2019) 最佳的wordpress在线聊天工具推荐插件 来帮你和潜在客户互动

    12 Best Live Chat Software for Small Business Compared (2019)     Did you know that more than 67% of ...

  5. 将RAC软件转换为单实例软件

    将RAC软件转换为单实例软件 http://blog.itpub.net/26736162/viewspace-2155632/ 1. Stop database and CRS on both no ...

  6. 21 go并发编程-下

    如何等待一组goroutine结束 1. 使用不带缓冲区的channel实现. 原理: 每个goroutine都往一个channel里写入一个值,然后我们去遍历这个管道的数值,由于不带缓冲区,那么必须 ...

  7. HTTP Status 404(The requested resource is not available)的几种解决方案

    1. 未部署Web应用 2.URL输入错误       排错方法:首先,查看URL的IP地址和端口号是否书写正确.其次,查看上下文路径是否正确 Project--------Properties--- ...

  8. MySQL 在高并发下的 订单撮合 系统使用 共享锁 与 排他锁 保证数据一致性

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  9. 下载Crypto,CyCrypto,PyCryptodome 报错问题

    python下载Crypto,CyCrypto,PyCryptodome,如有site-packages中存在crypto.pycrypto,在pip之前,需要pip3 uninstall crypt ...

  10. poj3335

    半平面交&多边形内核.因为没注意了点的情况自闭了. https://blog.csdn.net/qq_40861916/article/details/83541403 这个说的贼好. 多边形 ...