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 ...
随机推荐
- antd Grid
import { Row, Col } from 'antd'; <Row type="flex" //内容布局(左靠齐,右靠齐,居中) justify="star ...
- 一个较复杂的执行redis的lue脚本
- 通讯聊天工具(pingin)
pidgin需要添加插件才可以进行QQ,msn,facebook等聊天 sudo apt-get install pidgin(ubuntu 可以直接进行安装) sudo add-apt-reposi ...
- Chosen三级联动
上一篇介绍了 Chosen 的使用,这篇介绍联动.看代码: var addressResolve = function (options) { //检测用户传进来的参数是否合法 if (!isVal ...
- linux 安装pip 和python3
前言: python3应该是python的趋势所在,当然目前争议也比较大,这篇随笔的主要目的是记录在linux6.4下搭建python3环境的过程 以及碰到的问题和解决过程. 另外,如果本机安装了py ...
- Python中返回SQL字段名
def ReturnInfo(self, avalue, akey): cursor = connection.cursor() if type(avalue) == int: Sql = " ...
- Linux_脚本——使用echo从一个文件写入还有一个文件末尾
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/30980923 echo $(cat 你须要的文件) ...
- 本地测试时修改localhost为自己网站的域名的方法(转载)
做网站的,在本地测试时,所用的地址基本上都是localhost 或者直接用IP地址:127.0.0.1 如果仅仅是用来测试网站内部的程序代码之类的当然没问题,但是如果我们还要测试网站上添加的广告或者统 ...
- 搭建nlp_server服务器
这是文档 如何启动斯坦福NLP-Service 1.sudo apt-get install gearman-job-server安装gearman-server 2.启动gearman服务: gea ...
- 关于Git学习推荐
Git学习除了推荐官方网站:https://git-scm.com/之外, 我个人比较推荐初学者或者被动使用者可以学习参考廖雪峰的这个教程:https://www.liaoxuefeng.com/wi ...