15-oauth2+oidc实现Server部分
1-我们使用之前项目的mvcCookieAuthSampe2进行改造
1.1 增加IdentityServer4
2-增加Config.cs文件,对IdentityServer提供相关的配置数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Test;
using IdentityServer4.Models;
using IdentityServer4; namespace MvcCookieAuthSample
{
public class Config
{
public static IEnumerable<ApiResource> GetApiResources() {
return new List<ApiResource>() {
new ApiResource("api1","api DisplayName")
};
} public static IEnumerable<Client> GetClients()
{
return new List<Client>() {
new Client(){
ClientId="mvc",
AllowedGrantTypes= GrantTypes.Implicit,
ClientSecrets= new List<Secret>(){
new Secret("secret".Sha256())
},
RedirectUris = {"http://localhost:5001/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost/signout-callback-oidc"},
RequireConsent=false,
AllowedScopes={
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OpenId
}
}
};
} public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>() {
new IdentityResources.OpenId(),
new IdentityResources.Email(),
new IdentityResources.Profile()
};
} public static List<TestUser> GetTestUsers()
{
return new List<TestUser>() {
new TestUser(){
SubjectId="oa001",
Username="qinzb",
Password=""
}
};
} }
}
2-在Startup.cs文件启用IdentityServer
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddTestUsers(Config.GetTestUsers()) ;
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseIdentityServer(); //主要加了这段代码启用Identity4
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
3-在AccountController.cs提供登陆功能
private TestUserStore _testUserStore;
public AccountController(TestUserStore testUserStore)
{
_testUserStore = testUserStore;
} public IActionResult Login(string returnUrl = null)
{
ViewData["returnUrl"] = returnUrl;
return View();
} [HttpPost]
public async Task<IActionResult> Login(ViewModel.LoginViewModel loginModel, string returnUrl = null)
{
var findUser = _testUserStore.FindByUsername(loginModel.UserName);
// string returnUrl = Request.Form["returnUrl"];
if (findUser == null)
{
ModelState.AddModelError(nameof(loginModel.UserName), "用户不存在");
}
else
{
if (_testUserStore.ValidateCredentials(loginModel.UserName, loginModel.Password))
{
var profiles = new AuthenticationProperties()
{
IsPersistent = true,
ExpiresUtc = System.DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes())
}; await Microsoft.AspNetCore.Http.AuthenticationManagerExtensions.SignInAsync(HttpContext, findUser.SubjectId, findUser.Username, profiles); return string.IsNullOrEmpty(returnUrl) ? Redirect("/home/index") : Redirect(returnUrl);
}
ModelState.AddModelError(nameof(loginModel.Password), "密码不正确");
}
return View(); }
15-oauth2+oidc实现Server部分的更多相关文章
- 15.oauth2 + oidc 实现 server部分
OAuth主要做授权. OpenIdConnect简历在OAuth2.0基础之上的,相结合 客户端.授权中心.Resource Owner用户本身(资源的拥有者).Resource Server 通过 ...
- 【ASP.NET Core分布式项目实战】(二)oauth2 + oidc 实现 server部分
本博客根据http://video.jessetalk.cn/my/course/5视频整理(内容可能会有部分,推荐看源视频学习) 资料 我们基于之前的MvcCookieAuthSample来做开发 ...
- 16.oauth2 + oidc 实现 client部分
把授权和认证过的Server启动一下先 因为代码是之前的代码,所以有些代码需要清除一下 之类注释掉,因为这里暂时没有用到EFCode了 运行的时候发现一点错误 发现登陆的时候使用的RegisterVi ...
- ASP.NET Core分布式项目实战
ASP.NET Core开发者成长路线图 asp.net core 官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/ ...
- 【笔记目录1】ASP.NET Core分布式项目实战
当前标签: ASP.NET Core分布式项目实战 共2页: 1 2 下一页 35.Docker安装Mysql挂载Host Volume GASA 2019-06-20 22:02 阅读:51 评论 ...
- 使用OAuth Server PHP实现OAuth2服务
在现在的网络服务中,OAuth2.0服务已经很普遍了,无论是facebook或者微博的第三方登录,还是手机APP登录,都有很广泛的应用.它主要的目的如下:如果用户的照片在A网站,他想要在B网站使用A网 ...
- 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server
原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...
- 使用 OAuth2-Server-php 搭建 OAuth2 Server
Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...
- oauth2(转载http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A%A1)
http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A ...
随机推荐
- npm安装及webpack打包小demo
node(node.js) 安装 1.先从https://segmentfault.com/a/1190000004245357网站下载x64位的安装包node-v4.8.1-linux-x64.ta ...
- php curl 总结
1 curl post curl -X POST http://localhost/ -d '{"data":{"name":"hheh" ...
- jQuery解决高度统一问题
<div class="itemdl over"> <dl class="fl"> <dt><img src=&quo ...
- [原]Linux 修改时区
1.查看当前时区 date -R 2.修改当前时区 tzselect 之后会出来一个选项菜单,选择你想要的时区就OK了 3.替换系统时区文件 cp /usr/share/zoneinfo/XXX/YY ...
- JavaScript的DOM_处理空白节点
一.空白节点产生的原因 在非 IE6,7,8 中,标准的 DOM 具有识别空白文本节点的功能. 在火狐浏览器等其他浏览器中是 7个,而 IE6,7,8 自动忽略了,如果要保持一致的子元素节点,需要手 ...
- 弃坑pexpect,入坑paramiko
上文书说到,ssh库pexpect的使用,简直就是个“月亮公主”——满眼全是坑.勉强把程序写好了,跑起来的时候发现了一个新坑,让我不可抗拒的把它弃掉了——经常莫名其妙的连不上服务器!开线程连接14台服 ...
- SOJ3266 Birthday
Time Limit: 1000MS Memory Limit: 65536 K Description Today is Windy's birthday. What can I say? Inpu ...
- luogu P2617 Dynamic Rankings(分块,n <= 1e4)
嘟嘟嘟 带修改区间第k大. 然而某谷把数据扩大到了1e5,所以用分块现在只能得50分. 分块怎么做呢?很暴力的. 基本思想还是块内有序,块外暴力统计. 对于修改,直接重排修改的数所在块,时间复杂度O( ...
- Redis数据类型(下)
集合Set Redis 的集合不是 个线性结构,而是一个哈希表结构,它的内部会根据 hash 分子来 存储和查找数据,理论上 个集合可以存储 232 (大约 42 亿)个元素,因为采用哈希表结 ...
- MySQL 8.0.13的使用心得
今天在阿里云上安装了最新版的MySQL,把碰到的一些问题总结下 1.导入从另一台服务器dump的.sql,出现如下提示: ERROR at line xxx: Unknown command '\\' ...