Guest User

Untitled

a guest
Jan 20th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class BasicHttpAuthAttribute : ActionFilterAttribute
  2. {
  3. private readonly ILoginManager _manager;
  4.  
  5. public BasicHttpAuthAttribute(ILoginManager manager)
  6. {
  7. this._manager = manager;
  8. }
  9. public override void OnActionExecuting(HttpActionContext actionContext)
  10. {
  11. if (actionContext.Request.Headers.Authorization == null)
  12. {
  13. actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
  14. actionContext.Response.Content = new StringContent("Missing Auth-Token");
  15. }
  16. else
  17. {
  18.  
  19. var authToken = actionContext.Request.Headers.Authorization.Parameter;
  20. var decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authToken));
  21.  
  22. string userName = decodedToken.Substring(0, decodedToken.IndexOf(":"));
  23. string password = decodedToken.Substring(decodedToken.IndexOf(":") + 1);
  24.  
  25.  
  26. UserInfo user;
  27.  
  28.  
  29. if (_manager.LoginPasswordMatch(userName, password, out user))
  30. {
  31. var apiUser = new ApiUser(user.UserID);
  32. HttpContext.Current.User = new GenericPrincipal(new ApiIdentity(apiUser), new string[]{});
  33.  
  34. base.OnActionExecuting(actionContext);
  35.  
  36. }
  37. else
  38. {
  39. actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
  40. actionContext.Response.Content = new StringContent("Invalid username or password");
  41. }
  42.  
  43.  
  44.  
  45. }
  46. }
  47. }
  48.  
  49. var s = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IService));
Add Comment
Please, Sign In to add comment