遵义专业建站,app网站与普通网站的区别,安卓是哪里开发的,廉洁长沙网站概述IdentityServer4 是为ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 认证框架。将identityserver部署在你的应用中#xff0c;具备如下的特点可以为你的应用#xff08;如网站、本地应用、移动端、服务#xff09;做集中式的登录逻辑和工作流控制… 概述IdentityServer4 是为ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 认证框架。将identityserver部署在你的应用中具备如下的特点可以为你的应用如网站、本地应用、移动端、服务做集中式的登录逻辑和工作流控制。IdentityServer是完全实现了OpenID Connect协议标准。在各种类型的应用上实现单点登录登出。为各种各样的客户端颁发access token令牌,如服务与服务之间的通讯、网站应用、SPAS和本地应用或者移动应用等。OAuth 2.0 默认四种授权模式GrantType授权码模式authorization_code简化模式implicit密码模式password客户端模式client_credentials我们一般项目在api访问的时候大部分是基于账号密码的方式进行访问接口。比如app端的用户。下面我们来看下怎么实现密码模式password。主要实现方式1、在认证项目中创建ProfileService public class ProfileService : IProfileService{public async Task GetProfileDataAsync(ProfileDataRequestContext context){var claims context.Subject.Claims.ToList();context.IssuedClaims claims.ToList();}public async Task IsActiveAsync(IsActiveContext context){context.IsActive true;}}
2、创建ResourceOwnerPasswordValidator进行账号密码认证 public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator{public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context){//根据context.UserName和context.Password与数据库的数据做校验判断是否合法if (context.UserName conan context.Password 123){context.Result new GrantValidationResult(subject: context.UserName,authenticationMethod: custom,claims: new Claim[] { new Claim(Name, context.UserName), new Claim(UserId, 111), new Claim(RealName, conan), new Claim(Email, 373197550qq.com) });}else{//验证失败context.Result new GrantValidationResult(TokenRequestErrors.InvalidGrant, invalid custom credential);}}}
3、调整AllowedGrantTypes 和AllowedScopes client.AllowedGrantTypes GrantTypes.ResourceOwnerPassword;Liststring aas new Liststring();aas.AddRange(config.AllowedScopes);aas.Add(IdentityServerConstants.StandardScopes.OpenId);aas.Add(IdentityServerConstants.StandardScopes.Profile);client.AllowedScopes aas.ToArray();4、ConfigureServices增加AddInMemoryIdentityResources、AddResourceOwnerValidator、AddProfileService //注册服务var idResources new ListIdentityResource{new IdentityResources.OpenId(), //必须要添加否则报无效的 scope 错误new IdentityResources.Profile()};var p Configuration.GetSection(SSOConfig);services.AddIdentityServer().AddDeveloperSigningCredential().AddInMemoryIdentityResources(idResources).AddInMemoryApiResources(SSOConfig.GetApiResources(p)).AddInMemoryClients(SSOConfig.GetClients(p)).AddResourceOwnerValidatorResourceOwnerPasswordValidator().AddProfileServiceProfileService();services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest);5、在认证项目进行验证测试成功6、修改地址在网关项目进行认证测试成功代码地址https://gitee.com/conanOpenSource_admin/Example