2

我有一個Android應用程序,對學生的登錄表單,我想在網上API取決於身份驗證的Android應用程序來調用Web API服務

我已搜查在SQL Server中存儲的數據來檢查學生證件網絡和觀看許多影片,談論許多情況下,沒有什麼幫助我。

所有我想要的是我的休息服務自定義驗證(所以我應該向每個請求的憑據)

  • 我應該在asp.net的Web API服務做
  • 我怎麼能實現在Android應用程序

回答

1

我已經使用安全基本身份驗證,所以我應該提供的base64編碼

username:password

在標題爲每個請求如下

授權:基本「編碼的用戶名:密碼

httpGet.setHeader("Authorization", "Basic "+encodeUsernameAndPassword()); 

在服務器端我已實施的消息處理程序

public class BasicAuthenticationHandler : DelegatingHandler 
    { 
     public readonly IAuthenticationService authService; 
     public BasicAuthenticationHandler(IAuthenticationService service) 
     { 
      this.authService = service; 
     } 

     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
     { 
      AuthenticationHeaderValue authHeader = request.Headers.Authorization; 
      if (authHeader == null || authHeader.Scheme != "Basic") 
      { 
       return Unauthorized(request); 
      } 
      string encodedCredentials = authHeader.Parameter; 
      var credentialsBytes = Convert.FromBase64String(encodedCredentials); 
      var credentials = Encoding.ASCII.GetString(credentialsBytes).Split(':'); 

      if (!authService.Authenticate(credentials[0], credentials[1])) 
      { 
       return Unauthorized(request); 
      } 

      string[] roles = null;//todo 
      IIdentity identity = new GenericIdentity(credentials[0], "Basic"); 
      IPrincipal user = new GenericPrincipal(identity, roles); 


      HttpContext.Current.User = user; 


      return base.SendAsync(request, cancellationToken); 
     } 
3

似乎你沒有搜索「基於Web API令牌的身份驗證」;)無論如何,你需要實現是非常簡單的。 您需要使用OAuth 2.0資源所有者憑證流程,這意味着您只想爲特定終端提供用戶名/密碼一次,即(/令牌),然後如果用戶名/密碼有效,您獲得稱爲承載訪問令牌的東西。 該令牌在指定期限內有效,您可以在Web API中對此進行配置。 一旦你獲得訪問令牌,你需要將它安全地存儲在你的android應用程序中,然後使用授權標題(持有者方案(。 )寫下非常詳細的內容後覆蓋您的方案100%,請檢查後Token Based Authentication,讓我知道,如果你需要進一步的幫助。

相關問題