title: ASP.Net MVC basic authentication with membership
body: <p>I'm new to web development and particular to web API (rest service) development.
  I've studied how to develop MVC projects using entity framework and membership.
  When I needed to use authentication and authorization I simply added the AUTHORIZE attribute to the controller/action.
  In order to add user, I've used the Register action on the Account controller.</p>
  
  <p>Now I have a new project which is a web API project in MVC 5 (Visual studio 2013).
  I've added a new user using the default api action - Register (using fidler).
  Now, I'm trying to simply use a get method to read some data, but when adding the AUTHORIZE attribute and also add the correct user name and password I'm keep getting 401 (unauthorized) response.
  And then I've founded this thread:</p>
  
  <p><a href="https://stackoverflow.com/questions/11535075/asp-net-mvc-4-web-api-authentication-with-membership-provider">ASP.NET MVC 4 Web API Authentication with Membership Provider</a></p>
  
  <p>Bur now, the method Membership.ValidateUser(username, password) always returning false, I'm assuming I have to define the membership database, where do I do it?</p>
  
  <p>Here is the full code:</p>
  
  <pre><code>using System;
  using System.Net.Http;
  using System.Security.Principal;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Security;
  
  
  public class BasicAuthenticationMessageHandler : DelegatingHandler
  {
  protected override Task&lt;HttpResponseMessage&gt;         SendAsync(HttpRequestMessage     request, CancellationToken cancellationToken)
  {
      var authHeader = request.Headers.Authorization;
  
      if (authHeader == null)
      {
          return base.SendAsync(request, cancellationToken);
      }
  
      if (authHeader.Scheme != "Basic")
      {
          return base.SendAsync(request, cancellationToken);
      }
  
      var encodedUserPass = authHeader.Parameter.Trim();
      var userPass = Encoding.ASCII.GetString(Convert.FromBase64String(encodedUserPass));
      var parts = userPass.Split(":".ToCharArray());
      var username = parts[0];
      var password = parts[1];
  
      if (!Membership.ValidateUser(username, password))
      {
          return base.SendAsync(request, cancellationToken);
      }
  
      var identity = new GenericIdentity(username, "Basic");
      string[] roles = Roles.Provider.GetRolesForUser(username);
      var principal = new GenericPrincipal(identity, roles);
      Thread.CurrentPrincipal = principal;
      if (HttpContext.Current != null)
      {
          HttpContext.Current.User = principal;
      }
  
      return base.SendAsync(request, cancellationToken);
  }
  }
  </code></pre>
  
  <p>Here is where I've added the handler:</p>
  
  <pre><code>public class WebApiApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
          GlobalConfiguration.Configure(WebApiConfig.Register);
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          GlobalConfiguration.Configuration.MessageHandlers.Add(
  new BasicAuthenticationMessageHandler()
  );
      }
  }
  </code></pre>
  
  <p>Thanks in advance,
  Shaul</p>