回顾下ClientCredentials模式,在ReSourceApi中定义了咱们公开服务,第三方网站想要去访问ReSourceApi则须要在身份验证服务中获取toekn,根据token的内容,硬编码去访问公开服务(ResApi),这个仍是很是简单的,但!仔细思考下,咱们在客户端当中设置了对应的身份验证服务中心的地址,那么也就是能够有多对多的状况,固然咱们的第三方网站无需多言去关注这些。api
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.Authority = "https://localhost:5000"; options.RequireHttpsMetadata = false; options.ApiName = "api"; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
下面咱说下密码模式,这个模式安全级别比ClientCredentials高得多,第一步咱们须要修改一下咱们的Config文件.而后第二步就是添加咱们的TestUser对象.安全
public static IEnumerable<Client> GetClients()
{
return new List<Client> { new Client() { ClientId = "client", AllowedGrantTypes = GrantTypes.ClientCredentials,//客户端登录模式 ClientSecrets ={ new Secret("secret".Sha256()) }, AllowedScopes = {"api"} }, new Client() { ClientId = "pwdClient", AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,//密码受权登录模式 ClientSecrets ={ new Secret("secret".Sha256()) }, AllowedScopes = {"api"} } }; }
第二部TestUser对象由IdentityServer4.Test 给咱们提供了,咱们引入就ok,而后添加该方法用于测试.post
public static List<TestUser> GetTestUsers()
{
return new List<TestUser> { new TestUser() { SubjectId = "1", Username = "zara", Password = "112233" } }; }
固然,你还须要将测试数据注入到core中,咱们须要修改下原来的Stratup.cs类.测试
services.AddIdentityServer()//将Idserer DI到.netcore
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResource())//添加公开服务
.AddInMemoryClients(Config.GetClients())//客户端模拟数据
.AddTestUsers(Config.GetTestUsers());//用户测试数据
下面咱们用postMan来测试一下,先用原来的客户端模式,看,咱们对客户端模式不会影响。 网站
如今咱们再试一下密码登录模式,首先获取token!ui
咱们再去ReSourceApi中进行测试,OK,没问题!编码
如今咱们建立一个客户端,用于硬编码的密码登录。spa
using IdentityModel.Client;
using System; using System.Net.Http; namespace ThirdPartySolucation { public static class passWordLogin { public static void Login() { var diso = DiscoveryClient.GetAsync("https://localhost:5000").Result; if (diso.IsError) { Console.WriteLine(diso.Error); } var tokenClient = new TokenClient(diso.TokenEndpoint, "pwdClient", "secret"); var tokenResponse = tokenClient.RequestResourceOwnerPasswordAsync("zara","112233").Result; if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); } else { Console.WriteLine(tokenResponse.Json); } HttpClient httpClient = new HttpClient(); httpClient.SetBearerToken(tokenResponse.AccessToken); var response = httpClient.GetAsync("http://localhost:5001/api/values").Result; if (response.IsSuccessStatusCode) { Console.WriteLine(response.Content.ReadAsStringAsync().Result); } Console.WriteLine(); } } }
启动,结果以下:.net
若是须要不作secret验证,在Config中添加该参数:code
new Client()
{
ClientId = "pwdClient", AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets ={ new Secret("secret".Sha256()) }, RequireClientSecret = false, AllowedScopes = {"api"} }
上图是基本的客户端登录模式,而密码模式呢,则会在获取获取token中Body上下文中加入username,password来加以复杂认证,可是用密码也不太可靠,抽时间咱们说下受权码模式,说一说它们的区别与实现。