title: How use AuthorizationFilterAttribute in WebApi with WebClient Library?
body: <p>I use the following code for Authorization (I found it in internet and change it for my use)</p>
  
  <p>when i call my url seems authorization works 
  <img src="https://i.stack.imgur.com/CCqfO.jpg" alt="enter image description here"></p>
  
  <pre><code>    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
      public class ClientAuthorizationAttribute : AuthorizationFilterAttribute
      {
          private bool _active = true;
  
          public ClientAuthorizationAttribute()
          {
  
          }
  
          public ClientAuthorizationAttribute(bool active)
          {
              _active = active;
          }
  
          public override void OnAuthorization(HttpActionContext actionContext)
          {
              if (_active)
              {
                  var identity = ParseAuthorizationHeader(actionContext);
                  if (identity == null)
                  {
                      Challenge(actionContext);
                      return;
                  }
  
  
                  if (!OnAuthorizeUser(identity.Name, identity.Password, actionContext))
                  {
                      Challenge(actionContext);
                      return;
                  }
  
                  var principal = new GenericPrincipal(identity, null);
  
                  Thread.CurrentPrincipal = principal;
                  base.OnAuthorization(actionContext);
              }
          }
  
          protected virtual bool OnAuthorizeUser(string clientId, string authId, HttpActionContext actionContext)
          {
              return false;
          }
          protected virtual ClientAuthenticationIdentity ParseAuthorizationHeader(HttpActionContext actionContext)
          {
              string authHeader = null;
              var auth = actionContext.Request.Headers.Authorization;
              if (auth != null &amp;&amp; auth.Scheme == "Basic")
                  authHeader = auth.Parameter;
  
              if (string.IsNullOrEmpty(authHeader))
                  return null;
  
              authHeader = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader));
  
              var tokens = authHeader.Split(':');
              if (tokens.Length &lt; 2)
                  return null;
  
              return new ClientAuthenticationIdentity(tokens[0], tokens[1]);
          }
  
          void Challenge(HttpActionContext actionContext)
          {
              var host = actionContext.Request.RequestUri.DnsSafeHost;
              actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
              actionContext.Response.Headers.Add("WWW-Authenticate", string.Format("Basic realm=\"{0}\"", host));
          }
      }
  
      public class ClientAuthenticationIdentity : GenericIdentity
      {
          public ClientAuthenticationIdentity(string name, string password)
              : base(name, "Basic")
          {
              Password = password;
          }
          public string Password { get; set; }
      }
  
      public class BasicAuthorizationAttribute : ClientAuthorizationAttribute
      {
  
          public BasicAuthorizationAttribute()
          { }
  
          public BasicAuthorizationAttribute(bool active)
              : base(active)
          { }
  
  
          protected override bool OnAuthorizeUser(string clientId, string authId, HttpActionContext actionContext)
          {
              var businness = new WebServiceAuthBusiness();
              return businness.Count(x =&gt; x.ClientID == clientId &amp;&amp; x.AuthenticateID == authId) &gt; 0;
          }
      }
  }
  </code></pre>
  
  <p>in Client I use <strong>WebClient</strong> for Get application data (Does not work)</p>
  
  <pre><code>    [BasicAuthorization]
      public IList&lt;Application&gt; Get()
      {
          using (var client = new WebClient())
          {
  
              client.BaseAddress = _baseAddress;
              client.Encoding = Encoding.UTF8;
              client.UseDefaultCredentials = true; ???
              client.Credentials = new NetworkCredential(clientId, authId); ???
              var str = client.DownloadString("api/application/get");
              return JsonConvert.DeserializeObject&lt;List&lt;Application&gt;&gt;(str);
          }
      }
  </code></pre>
  
  <p>How i can send username and password with webClient for AuthorizationFilter ???</p>