带有身份验证的MvvmCross HTTP DownloadCache [英] MvvmCross HTTP DownloadCache with authentication

查看:82
本文介绍了带有身份验证的MvvmCross HTTP DownloadCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,需要在服务器上对用户进行身份验证才能使用WebAPI下载数据. MvvmCross DownloadCache插件似乎只处理基本的HTTP GET查询.我无法在网址中添加身份验证令牌,因为它是一个很大的SAML令牌.

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.

如何为通过DownloadCache插件完成的查询添加HTTP标头?

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

在当前版本中,我认为我应该注入自己的IMvxHttpFileDownloader,但我正在寻找一个更简单的解决方案.注入我自己的MvxFileDownloadRequest会更好(虽然不完美),但是它没有接口...

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...

推荐答案

我能够为自定义方案(http-auth://)注册自定义IWebRequestCreate.

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

从我的数据源转换url有点丑陋,但确实可以.

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

这篇关于带有身份验证的MvvmCross HTTP DownloadCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆