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. zan扩展安装

    官方地址 https://github.com/youzan/zan //提示缺少libcurl扩展时候安装 yum install libcurl-devel //安装完zan.so php -m提 ...

  2. U-Mail邮件群发:邮件营销最全建议

    U-Mail专注于邮件营销平台研发工作多年了,服务企业数千家,拥有上万IP,在国内外基础设施建设上投资巨大,技术团队精湛.客服人员热情,赢 得了业界的好评和用户信任.有一些用户给我们发来邮件或来电咨询 ...

  3. Java基础之二维数组的回顾

    class ArrayWork { /* * 二维数组的复习! * * 2014年4月2日 21:45:50 * * * **/ public static void main(String[] ar ...

  4. 关于Java中的反射的一个简单使用

    把以前在其他地方的技术文章重新整理一遍, 方便自己回忆, 也方便他人借鉴. 刚工作的时候发过这么一段代码: package cn.com.hanbinit.test; import java.lang ...

  5. Django 模型中FileField字段

    FileField¶ class FileField([upload_to=None, max_length=100, **options])¶ 一个上传文件的字段. 注意 FileField字段不支 ...

  6. 【转】2013 PHP技术峰会《Bug Free的PHP开发实践分享》摘录

    要想代码写的好,前提配置做的好 error_reporting  =  E_ALL | E_STRICT display_errors = 测试机设置为 On,生产机设置为 Off display_s ...

  7. 【[NOI2016]区间】

    发现自己的离散化姿势一直有问题 今天终于掌握了正确的姿势 虽然这并不能阻挡我noip退役爆零的历史进程 还是先来看看离散化怎么写吧,我以前都是这么写的 for(std::set<int>: ...

  8. MyBatis(2)增删改查

    本次全部学习内容:MyBatisLearning   查: 根据id查询用户信息,得到一个用户信息   在User.xml文件中添加代码: <mapper namespace="tes ...

  9. selenium基础知识(概述、安装、IDE等)

    参考http://www.yiibai.com/selenium/selenium_webdriver.html

  10. form表单上传文件

    一.formData()直接获取form表单数据 例子:获取form表单的id给formData(),然后传给后台. 要求: 传入值的name值必须与后台接受的name相对应. form表单不能嵌套, ...