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(); }

最新文章

  1. thinkPHP get
  2. Coursera-Getting and Cleaning Data-week4-R语言中的正则表达式以及文本处理
  3. 【python】确保文件写入结束
  4. SoapUI中如何传递cookie
  5. Hbuild - 使用海马玩模拟器调试
  6. nslayoutConstraint
  7. c++ ip地址的操作 c版
  8. 使用var声明的变量 和 直接赋值并未声明的变量的区别
  9. PS4破解
  10. textBox只能输入汉字
  11. ADT &quot;Running Android Lint&quot; has encountered a problem
  12. make clean与make distclean的区别
  13. AngularJS指南文档
  14. scp简单使用
  15. mysql配置优化浅谈(一)
  16. Dynamics 365 Online-Relevance Search
  17. 【XSY2767】朋友 广义后缀自动机 网络流
  18. jQuery 学习笔记(5)(事件绑定与解绑、事件冒泡与事件默认行为、事件的自动触发、自定义事件、事件命名空间、事件委托、移入移出事件)
  19. 【原创】Team Foundation Server 域环境迁移
  20. 【拓扑 &amp;&amp; 模板】Kosaraju算法

热门文章

  1. July 23rd 2017 Week 30th Sunday
  2. java 开发常用IDE
  3. linux 上部署tomcat的java web项目,会出现post提交request.request.getParameter()的得不到值的情况
  4. Powershell split file
  5. PHP----练习-----新闻管理----增删改查
  6. MySQL数据库------常用函数
  7. 通过nginx 499 来判断服务端超时数量
  8. [19/04/03-星期三] IO技术_其它流(RandomAccessFile 随机访问流,SequenceInputStream 合并流)
  9. 关于js点击事件出现 xx is not defined at HTMLAnchorElement.onclick 的问题
  10. android中OpenMax的实现【3】OMX中主要成员