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部分的更多相关文章

  1. 15.oauth2 + oidc 实现 server部分

    OAuth主要做授权. OpenIdConnect简历在OAuth2.0基础之上的,相结合 客户端.授权中心.Resource Owner用户本身(资源的拥有者).Resource Server 通过 ...

  2. 【ASP.NET Core分布式项目实战】(二)oauth2 + oidc 实现 server部分

    本博客根据http://video.jessetalk.cn/my/course/5视频整理(内容可能会有部分,推荐看源视频学习) 资料 我们基于之前的MvcCookieAuthSample来做开发 ...

  3. 16.oauth2 + oidc 实现 client部分

    把授权和认证过的Server启动一下先 因为代码是之前的代码,所以有些代码需要清除一下 之类注释掉,因为这里暂时没有用到EFCode了 运行的时候发现一点错误 发现登陆的时候使用的RegisterVi ...

  4. ASP.NET Core分布式项目实战

    ASP.NET Core开发者成长路线图 asp.net core 官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/ ...

  5. 【笔记目录1】ASP.NET Core分布式项目实战

    当前标签: ASP.NET Core分布式项目实战 共2页: 1 2 下一页  35.Docker安装Mysql挂载Host Volume GASA 2019-06-20 22:02 阅读:51 评论 ...

  6. 使用OAuth Server PHP实现OAuth2服务

    在现在的网络服务中,OAuth2.0服务已经很普遍了,无论是facebook或者微博的第三方登录,还是手机APP登录,都有很广泛的应用.它主要的目的如下:如果用户的照片在A网站,他想要在B网站使用A网 ...

  7. 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server

    原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...

  8. 使用 OAuth2-Server-php 搭建 OAuth2 Server

    Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...

  9. 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 ...

随机推荐

  1. DQN核心思想理解

    看过Deep learning(convolutional neural network),看过RL(Q-learning).但是在两者结合这一块一直弄不明白. 我的疑问在于一直不明白DL是怎样识别出 ...

  2. CopyOnWriteArrayList对比ArrayList

    ArrayList非线程安全,CopyOnWriteArrayList线程安全 ArrayList添加元素的时候内部会预先分配存储空间,CopyOnWriteArrayList每次添加元素都会重新co ...

  3. dom4j.jar有什么作用?

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源代 ...

  4. BZOJ1009:[HNOI2008]GT考试(AC自动机,矩乘DP)

    Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2...Am(0< ...

  5. 「LG4782 模板 2-SAT 问题」

    题目 来学\(2\)-\(sat\)了 这个东西确实不难 这个算法就是给你一堆\(bool\)变量\(x_1,x_2...x_n\),之后给你一些限制 限制的形式就是给你一对\((u,o1,v,o2) ...

  6. 浅析webpack打包输出内容

    当我们执行npm run bundle的时候输出了很多信息,那么这些信息都是什么意思呢 Hash: 221e7fd2e8bf82149df7 Version: webpack 4.30.0 Time: ...

  7. JDBC(1)简单介绍/数据库的连接

    初识JDBC: JDBC是java连接数据库的一个工具,没有这个工具,java将无法和数据库进行连接. JDBC API: JDBC是个“低级”接口,也就是说,他直接用于调用SQL命令. JDBC驱动 ...

  8. java8的4大核心函数式接口

    //java8的4大核心函数式接口//1.Consumer<T>:消费性接口//需求:public void happy(double money, Consumer<Double& ...

  9. 使用Spring实现AOP(XML+注解)

    一.Spring对AOP的支持 AOP并不是Spring框架特有的,Spring只是支持AOP编程的框架之一,每一个框架对AOP的支持各有特点,有些AOP能够对方法的参数进行拦截,有些AOP对方法进行 ...

  10. CSS布局方面的一些小总结

    1. display属性 display是CSS布局的第一站,它控制一个元素以什么“身份”出现在页面布局当中.它的值有很多个,常用的有block,inline,inline-block,table,n ...