Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BasicHttpAuthAttribute : ActionFilterAttribute
- {
- private readonly ILoginManager _manager;
- public BasicHttpAuthAttribute(ILoginManager manager)
- {
- this._manager = manager;
- }
- public override void OnActionExecuting(HttpActionContext actionContext)
- {
- if (actionContext.Request.Headers.Authorization == null)
- {
- actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
- actionContext.Response.Content = new StringContent("Missing Auth-Token");
- }
- else
- {
- var authToken = actionContext.Request.Headers.Authorization.Parameter;
- var decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authToken));
- string userName = decodedToken.Substring(0, decodedToken.IndexOf(":"));
- string password = decodedToken.Substring(decodedToken.IndexOf(":") + 1);
- UserInfo user;
- if (_manager.LoginPasswordMatch(userName, password, out user))
- {
- var apiUser = new ApiUser(user.UserID);
- HttpContext.Current.User = new GenericPrincipal(new ApiIdentity(apiUser), new string[]{});
- base.OnActionExecuting(actionContext);
- }
- else
- {
- actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
- actionContext.Response.Content = new StringContent("Invalid username or password");
- }
- }
- }
- }
- var s = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IService));
Add Comment
Please, Sign In to add comment