title: Windows Phone 8 Basic Authentication
body: <p>I have hosted a Windows Web API using a basic authentication header with username and password.</p>
  
  <p>I'm trying to create a login form that takes a username and password and sends back a token. 
  so i have the following code.</p>
  
  <p>I'm using a Attributed method </p>
  
  <pre><code> public class BasicAuthenticationAttribute : System.Web.Http.Filters.ActionFilterAttribute
  {
  
      private IPromiseRepository promiseRepository;
  
  
      public BasicAuthenticationAttribute()
      {
  
  
          this.promiseRepository = new EFPromiseRepository(new PropellorContext());
          //repository = promiseRepository;
      }
  
      public BasicAuthenticationAttribute(IPromiseRepository promiseRepository, INewsFeedRepository newsfeedRepository)
      {
          this.promiseRepository = promiseRepository;
  
      }
  
  
  
      public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
      {
  
          if (actionContext.Request.Headers.Authorization == null)
          {
              actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
          }
  
          else
          {
  
  
              string authToken = actionContext.Request.Headers.Authorization.Parameter;
  
              string decodedToken = authToken;
              //    Encoding.UTF8.GetString(Convert.FromBase64String(authToken));
  
              string username = decodedToken.Substring(0, decodedToken.IndexOf(":"));
              string password = decodedToken.Substring(decodedToken.IndexOf("^")+1);
              string APIToken = decodedToken.Substring(decodedToken.IndexOf("="));
              APIToken = APIToken.Replace("=", string.Empty);
  
              password = password.Replace("=", string.Empty);
  
  
              if (!string.IsNullOrEmpty(APIToken))
              {
                  password = password.Replace(APIToken, string.Empty);
              }
  
  
  
              if (username != null &amp;&amp; password != null)
              {
                  try
                  {
                      var user = promiseRepository.GetUserByName(username);
                      var salt = user.PasswordSalt;
  
                      System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();
                      Byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(password, salt));
                      Byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);
                      HashTool.Clear();
                      var hashedpass = Convert.ToBase64String(EncryptedBytes);
  
  
                      if (hashedpass == user.Password)
                      {
  
                          if (string.IsNullOrEmpty(user.APIToken))
                          {
                              String guid = System.Guid.NewGuid().ToString();
                              user.APIToken = guid;
                              promiseRepository.UpdateUser(user);
                              promiseRepository.Save();
                          }
  
  
                          if (user != null)
                          {
  
                              user = promiseRepository.GetUserByUserID(user.UserID);
                              HttpContext.Current.User = new GenericPrincipal(new ApiIdentity(user), new string[] { });
  
  
                              base.OnActionExecuting(actionContext);
                          }
  
                      }
  
                      if (APIToken != null)
                      {
                          if (user.APIToken == APIToken)
                          {
                              var userbytoken = promiseRepository.GetUserByAPIToken(APIToken);
                              HttpContext.Current.User = new GenericPrincipal(new ApiIdentity(userbytoken), new string[] { });
  
  
                              base.OnActionExecuting(actionContext);
                          }
                      }
  
                  }
  
  
                  catch (Exception)
                  {
  
                      {
                          actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
                          base.OnActionExecuting(actionContext);
                      }
                      throw;
                  }
  
  
  
  
  
  
  
  
              }
  
  
  
          }
  
  
      }
  
  
  
      }
  </code></pre>
  
  <p>This works with Fiddler when the correct credentials are passed </p>
  
  <p>I'm attempting to produce the same authentication in my windows phone application.</p>
  
  <p>Passes a username and password into the basic authentication http header.</p>
  
  <p>However I'm not sure how to do this after a large amount of diggging on the internet alot of the exmaples are windows phone 7 and certain methods don't exist anymore.</p>
  
  <p>This is the code i have arrived at.</p>
  
  <pre><code>private void Login1_Click(object sender, RoutedEventArgs e)
  
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:5650/api/start");
  
          NetworkCredential credentials = new NetworkCredential(userName.Text + ":^",password.Text + "=");
          request.Credentials = credentials;
  
          request.BeginGetResponse(new AsyncCallback(GetSomeResponse), request);
  </code></pre>
  
  <p>Hopefully someone can guide me into the right direction.
  it should be simple in principle :( </p>