回顧下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來加以複雜認證,可是用密碼也不太可靠,抽時間咱們說下受權碼模式,說一說它們的區別與實現。