【问题标题】:MvvmCross HTTP DownloadCache with authentication带有身份验证的 MvvmCross HTTP 下载缓存
【发布时间】:2014-01-13 14:32:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: httpwebrequest xamarin mvvmcross


    【解决方案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;
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 2017-03-02
      • 2018-01-24
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多