Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

httpwebrequest - MvvmCross HTTP DownloadCache with authentication

In my app, the user need to be authenticated on the server to download data using WebAPIs. The MvvmCross DownloadCache plugin seems to handle only basic HTTP GET queries. I can't add my authentication token in the url as it's a big SAML token.

How can I add a HTTP header to queries done through DownloadCache plugin ?

With the current version I think I should inject my own IMvxHttpFileDownloader but I'm looking for an easier solution. Injecting my own MvxFileDownloadRequest would be better (not perfect) but it doesn't have an interface...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm able to do it registering a custom IWebRequestCreate for a custom scheme (http-auth://).

It's a bit ugly to transform urls from my datasource but it does the job.

  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;
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...