ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(使用官网提供的UI)
IdentityServer官方提供web页面,可以根据需求修改样式。具体UI下载跟配置参考官网文档。
文档地址:https://identityserver4.readthedocs.io/en/release/quickstarts/3_interactive_login.html
使用UI登录涉及的知识点参考园里大佬@solenovex的文章。这里不在重复说明,感兴趣的同学可以去看看。原文地址:https://www.cnblogs.com/cgzl/p/9253667.html
我这里是根据官方提供的UI进行修改、自定义用户登录、访问API。
源码地址:https://github.com/YANGKANG01/IdentityServer4-IdentityAuth
一、配置IdentityServer
1、引入官网包:IdentityServer4
2、需要重新定义Web端信息,除了ClientId,AllowedGrantTypes,ClientSecrets,AllowedScopes等属性还需要配置登录成功或注销后的返回地址RedirectUris跟PostLogoutRedirectUris。Config文件配置如下:
using IdentityServer4;
using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace IdentityServer
{
/// <summary>
/// IdentityServer配置
/// </summary>
public class Config
{
/// <summary>
/// 添加对OpenID Connect的支持
/// </summary>
/// <returns></returns>
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(), //必须要添加,否则报无效的scope错误
new IdentityResources.Profile()
};
}
/// <summary>
/// 定义系统中的API资源
/// </summary>
/// <returns></returns>
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
} /// <summary>
/// 客户端访问资源
/// </summary>
/// <returns></returns>
public static IEnumerable<Client> GetClients()
{
// 客户端信息
return new List<Client>
{
//自定义接口登录的客户端
new Client
{
//客户端ID名称
ClientId = "client1",
//客户端访问方式:密码验证
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
//用于认证的密码加密方式
ClientSecrets =
{
new Secret("secret".Sha256())
},
//客户端有权访问的范围
AllowedScopes = { "api1",
IdentityServerConstants.StandardScopes.OpenId, //必须要添加,否则报403 forbidden错误
IdentityServerConstants.StandardScopes.Profile
}
},
//定义mvc客户端
new Client
{
//客户端ID名称
ClientId = "mvc",
ClientName = "MVC Client",
//访问类型
AllowedGrantTypes = GrantTypes.Hybrid,
//关闭确认是否返回身份信息界面
RequireConsent=false,
// 登录成功后重定向地址
RedirectUris = { "http://localhost:5002/signin-oidc" },
//注销成功后的重定向地址
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
//用于认证的密码加密方式
ClientSecrets =
{
new Secret("secret".Sha256())
},
//客户端有权访问的范围
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1",//要访问的api名称
},
}
};
}
}
}
3、Startup的配置跟前面自定义接口登录一样没什么变化,这里也贴下源码方便没看前篇的同学:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; namespace IdentityServer
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
//配置身份服务器与内存中的存储,密钥,客户端和资源
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())//用户信息建模
.AddInMemoryApiResources(Config.GetApiResources())//添加api资源
.AddInMemoryClients(Config.GetClients())//添加客户端
.AddResourceOwnerValidator<LoginValidator>()//用户校验
.AddProfileService<ProfileService>();
//注册mvc服务
services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//添加到HTTP管道中。
app.UseIdentityServer();
//添加静态资源访问
app.UseStaticFiles();
//添加mvc到管道中
app.UseMvcWithDefaultRoute(); }
}
}
4、添加官网UI后找到AccountController类,修改登录方法Login ,把下图中的代码替换成自己的查询数据库用户即可,这里官方使用的是TestUsers类中自定义的用户来登录。

二、Web端配置获取登录后的用户信息
1、引入官网包:IdentityModel。
2、Startup配置如下。ResponseType返回类型详情可参考@solenovex的文章,大佬在文章说的比较详细,这里不在重复说明,地址:https://www.cnblogs.com/cgzl/p/9253667.html
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; namespace Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
//授权端服务地址
options.Authority = "http://localhost:5000/";
//是否https请求
options.RequireHttpsMetadata = false;
//客户端ID名称
options.ClientId = "mvc";
options.ClientSecret = "secret";
//返回的类型
options.ResponseType = "code id_token";
//添加自定义用户信息
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
//是否存储token
options.SaveTokens = true;
//用于设置在从令牌端点接收的id_token创建标识后,处理程序是否应转到用户信息端点
options.GetClaimsFromUserInfoEndpoint = true;
//访问名称api
options.Scope.Add("api1");
//避免claims被默认过滤掉,如果不想让中间件过滤掉nbf和amr, 把nbf和amr从被过滤掉集合里移除。可以使用下面这个方方式:
options.ClaimActions.Remove("nbf");
options.ClaimActions.Remove("amr");
//删除某些Claims
options.ClaimActions.DeleteClaim("sid");
options.ClaimActions.DeleteClaim("idp");
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles();
app.UseCookiePolicy();
//添加用户验证中间件
app.UseAuthentication();
//
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
3、在需要登录的页面方法添加Authorize特性。

启动web 会看到页面自动重定向到登录页面效果如图:

4、获取用户信息可使用HttpContext.User.Identity来获取当前用户信息,代码如下:
//获取用户信息
var claimIdentity = (ClaimsIdentity)HttpContext.User.Identity;
var claimsPrincipal = claimIdentity.Claims as List<Claim>;
5、获取用户token,这里的token是服务端生成发送给客户端,用于身份校验,可使用token访问需要登录权限的API接口。代码如下:
//获取用户token
var token = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken);
Ps:注销登录默认跳转到退出信息提示页面,如不需要修改服务端的 AccountOptions类中的AutomaticRedirectAfterSignOut属性,默认为false,修改为true即可。
三、API配置
1、引入官网包:IdentityServer4.AccessTokenValidation。
2、Startup类配置如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; namespace API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddAuthorization()//添加身份验证服务
.AddJsonFormatters();
//AddAuthentication将身份验证服务添加到DI并配置"Bearer"为默认方案
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";//identifyServer服务地址
options.RequireHttpsMetadata = false;//是否使用https options.ApiName = "api1";//进行身份验证的API资源的名称
});
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//将身份验证中间件添加到管道中
app.UseAuthentication(); app.UseMvc();
}
}
}
3、给对应的API接口添加Authorize特性,Web端登录通过token访问该接口。代码如下:
//获取用户token
var token = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken);
//实例化HttpClient
var client = new HttpClient();
client.SetBearerToken(token);
//请求identity接口
var response = await client.GetAsync("http://localhost:5001/identity");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(使用官网提供的UI)的更多相关文章
- ASP.NET Core的身份认证框架IdentityServer4(3)-术语的解释
IdentityServer4 术语 IdentityServer4的规范.文档和对象模型使用了一些你应该了解的术语. 身份认证服务器(IdentityServer) IdentityServer是一 ...
- ASP.NET Core的身份认证框架IdentityServer4(7)- 使用客户端证书控制API访问
前言 今天(2017-9-8,写于9.8,今天才发布)一口气连续把最后几篇IdentityServer4相关理论全部翻译完了,终于可以进入写代码的过程了,比较累.目前官方的文档和Demo以及一些相关组 ...
- ASP.NET Core的身份认证框架IdentityServer4(1)-特性一览
IdentityServer4是ASP.NET Core的一个包含OpenID和OAuth 2.0协议的框架.OpenID和OAuth 的区别请看 https://www.zhihu.com/ques ...
- ASP.NET Core的身份认证框架IdentityServer4(6)- 开始
安装和概述 启动一个新的IdentityServer项目有两种基本方法: 从头开始 从Visual Studio中的ASP.NET身份模板开始 如果从头开始,我们提供了一些文档.项目帮助和内存存储支持 ...
- ASP.NET Core的身份认证框架IdentityServer4(5)- 包和构建
包和构建 IdentityServer有许多nuget包 IdentityServer4 nuget | github 包含IdentityServer核心对象模型,服务和中间件. 仅支持内存配置和用 ...
- ASP.NET Core的身份认证框架IdentityServer4(9)-使用OpenID Connect添加用户认证
OpenID Connect OpenID Connect 1.0是OAuth 2.0协议之上的一个简单的身份层. 它允许客户端基于授权服务器执行的身份验证来验证最终用户的身份,以及以可互操作和类似R ...
- ASP.NET Core的身份认证框架IdentityServer4--(4)添加第三方快捷登录
添加对外部认证的支持 接下来我们将添加对外部认证的支持.这非常简单,因为你真正需要的是一个兼容ASP.NET Core的认证处理程序. ASP.NET Core本身也支持Google,Facebook ...
- ASP.NET Core的身份认证框架IdentityServer4(8)- 使用密码认证方式控制API访问
前言 本文及IdentityServer这个系列使用的都是基于.net core 2.0的.上一篇博文在API项目中我使用了icrosoft.AspNetCore.Authentication.Jwt ...
- ASP.NET Core的身份认证框架IdentityServer4(4)- 支持的规范
IdentityServer实现以下规范: OpenID Connect OpenID Connect Core 1.0 (spec) OpenID Connect Discovery 1.0 (sp ...
随机推荐
- Java文件写入时是否覆盖
这个是和服务器读数据结合着来的,是向服务器文件写数据,这就碰到了是否覆盖以前写的数据的问题,看FileWriter();的参数后面的参数名叫append,用词典查是附加的意思,灵机一动,改成false ...
- 第二篇——VC++简单随机四则运算
目标:编写最简单的四则运算,类似A+B=C: 想法:建立一个Win32控制台应用程序,A和B用随机数表示,运算符号用0~3的数字对应,然后计算并输出即可: 具体过程: 利用函数rand(),返回一个0 ...
- bata5
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
- WPF里面制作圆角文本框
转自:http://www.cnblogs.com/mengxin523/archive/2010/04/04/1704448.html 本以为WPF里面的XAML会很强大,可以设置很多属性,比如文本 ...
- ASP.NET MVC 5.0 参考源码索引
http://www.projky.com/asp.netmvc/5.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...
- JS面向对象(封装,继承)
在六月份找工作中,被问的最多的问题就是: js面向对象,继承,封装,原型链这些,你了解多少? 额,,,我怎么回答呢, 只能说,了解一些,不多不少,哈哈哈哈,当然,这是玩笑话. 不过之前学过java,来 ...
- [微软官方]FSUTIL
Applies To: Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7, Windows Server 2003 ...
- 浏览器本地存储(browser-storage)
https://www.baidufe.com/component/browser-storage/api.html 首页 | API参考 | 升级日志 BrowserStorage.api.set( ...
- java传统的文件拷贝 相当于两个大缸需要通过一个勺子(字节数组)一点一点运过去