天天看點

asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)

用戶端模式定義

使用者向用戶端提供使用者名密碼。用戶端使用這些資訊,向“服務提供商” 進行認證。

先上密碼模式的工作流程圖:

asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)

密碼模式和用戶端模式差不多,但是此時User已經非常信任Client,以至于連User的認證密碼都給Client;

這通常用在使用者對用戶端高度可信的情況下出現這種情況。 認證伺服器隻有在其他授權模式無法執行的情況下,才能考慮使用這種模式

注意:這種認證模式預設還是需要用戶端模式支援的哦;即在認證過程中同樣會認證Client的權限;

而且,這種認證模式,需要認證的資源(Resource)已經和User有關系了哦。

AuthorizationServer與ResourceServer還是用上一講的項目

建立項目ResourceOwnerPasswordCredentialGrant

asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)
    static void Main(string[] args)             {                 const string url = "http://localhost:8270/";                 var client = new HttpClient();                 var clientId = "123456";                 var clientSecret = "abcdef";                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(                     "Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(clientId + ":" + clientSecret)));                 // 密碼模式                 var parameters = new Dictionary<string, string>();                 parameters.Add("grant_type", "password");                 parameters.Add("UserName", "UserName");                 parameters.Add("Password", "Password");                 // 送出                 var result = client.PostAsync(url + "OAuth/Token", new FormUrlEncodedContent(parameters)).Result.Content.ReadAsStringAsync().Result;                 var entity = JsonConvert.DeserializeObject<Token>(result);                 Console.WriteLine("Token: {0}", entity.AccessToken);                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", entity.AccessToken);                 var result2 = client.GetAsync("http://localhost:8001/api/Values").Result.Content.ReadAsStringAsync().Result;                 Console.WriteLine("Result: {0}", result2);                 Console.ReadKey();             }      

注意這幾句哦:

var clientId = "123456";           // 用戶端的認證密碼(即預設還是需要認證用戶端的權限的哦)     var clientSecret = "abcdef";      
。。。     parameters.Add("grant_type", "password");  // 指定服務端以密碼模式認證      
parameters.Add("UserName", "UserName");    // User的認證密碼      
parameters.Add("Password", "Password");      

 用戶端有效代碼就這些了,我再來看看服務端需要哪些更改

 1、Startup.Auth.cs檔案修改OnGrantResourceOwnerCredentials = GrantResourceOwnerCredentials

 這個方法就是背景處理密碼模式認證關鍵的地方

asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)

我們看看他的實作過程,也很簡單的

asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)

OK,我們來看看運作效果;

asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)

 認證成功!是不是很簡單呀?

asp.net權限認證系列

  1. asp.net權限認證:Forms認證
  2. asp.net權限認證:HTTP基本認證(http basic)
  3. asp.net權限認證:Windows認證
  4. asp.net權限認證:摘要認證(digest authentication)
  5. asp.net權限認證:OWIN實作OAuth 2.0 之用戶端模式(Client Credential)
  6. asp.net權限認證:OWIN實作OAuth 2.0 之密碼模式(Resource Owner Password Credential)
  7. asp.net權限認證:OWIN實作OAuth 2.0 之授權碼模式(Authorization Code)
  8. asp.net權限認證:OWIN實作OAuth 2.0 之簡化模式(Implicit)

繼續閱讀