2014-01-13 3 views
0

내 앱에서 사용자는 WebAPI를 사용하여 데이터를 다운로드하려면 서버에서 인증을 받아야합니다. MvvmCross DownloadCache 플러그인은 기본 HTTP GET 쿼리 만 처리하는 것으로 보입니다. 큰 SAML 토큰이므로 URL에 인증 토큰을 추가 할 수 없습니다.MvvmCross HTTP DownloadCache with authentication

DownloadCache 플러그인을 통해 수행 된 쿼리에 HTTP 헤더를 어떻게 추가 할 수 있습니까?

현재 버전에서는 내 자신의 IMvxHttpFileDownloader를 주입해야한다고 생각하지만 더 쉬운 해결책을 찾고 있습니다. 내 자신의 MvxFileDownloadRequest를 주입하는 것이 더 좋을 것입니다 (완벽하지는 않지만).하지만 인터페이스가 없습니다 ...

답변

1

사용자 정의 스키마 (http-auth : //)에 대한 사용자 정의 IWebRequestCreate를 등록 할 수 있습니다.

내 데이터 소스에서 URL을 변환하는 것은 다소 못 생겼지 만 그 일을합니다.

public class AuthenticationWebRequestCreate : IWebRequestCreate 
    { 
    public const string HttpPrefix = "http-auth"; 
    public const string HttpsPrefix = "https-auth"; 

    private static string EncodeCredential(string userName, string password) 
    { 
     Encoding encoding = Encoding.GetEncoding("iso-8859-1"); 
     string credential = userName + ":" + password; 
     return Convert.ToBase64String(encoding.GetBytes(credential)); 
    } 

    public static void RegisterBasicAuthentication(string userName, string password) 
    { 
     var authenticateValue = "Basic " + EncodeCredential(userName, password); 
     AuthenticationWebRequestCreate requestCreate = new AuthenticationWebRequestCreate(authenticateValue); 
     Register(requestCreate); 
    } 

    public static void RegisterSamlAuthentication(string token) 
    { 
     var authenticateValue = "SAML2 " + token; 
     AuthenticationWebRequestCreate requestCreate = new AuthenticationWebRequestCreate(authenticateValue); 
     Register(requestCreate); 
    } 

    private static void Register(AuthenticationWebRequestCreate authenticationWebRequestCreate) 
    { 
     WebRequest.RegisterPrefix(HttpPrefix, authenticationWebRequestCreate); 
     WebRequest.RegisterPrefix(HttpsPrefix, authenticationWebRequestCreate); 
    } 

    private readonly string _authenticateValue; 

    public AuthenticationWebRequestCreate(string authenticateValue) 
    { 
     _authenticateValue = authenticateValue; 
    } 

    public WebRequest Create(System.Uri uri) 
    { 
     UriBuilder uriBuilder = new UriBuilder(uri); 
     switch (uriBuilder.Scheme) 
     { 
     case HttpPrefix: 
      uriBuilder.Scheme = "http"; 
      break; 
     case HttpsPrefix: 
      uriBuilder.Scheme = "https"; 
      break; 
     default: 
      break; 
     } 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri); 
     request.Headers[HttpRequestHeader.Authorization] = _authenticateValue; 
     return request; 
    } 
    }