public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.Request.Headers.Authorization == null) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); } else { string authenticationToken = actionContext.Request.Headers.Authorization.Parameter; string decodedAuthenticationToken = Encoding.UTF8.GetString( Convert.FromBase64String(authenticationToken)); string[] usernamePasswordArray = decodedAuthenticationToken.Split(':'); string username = usernamePasswordArray[0]; string password = usernamePasswordArray[1]; UserNameCollection userNameCollection = UserNameCollection.GetInstance(); if (userNameCollection.Login(username, password)) { Thread.CurrentPrincipal = new GenericPrincipal( new GenericIdentity(username), null); } else { actionContext.Response = actionContext.Request .CreateResponse(HttpStatusCode.Unauthorized); } } }
//singleton pattern used for this class public static UserNameCollection GetInstance() { if (instance == null) { instance = new UserNameCollection(); } return(instance); }