public UserModel GetUserInformationById(int userId) { try { var userData = new Business.UserMethods().GetUserInformationBasedOnId(userId); if (userData == null) throw new EntryNotFound(); return userData; } catch (EntryNotFound ex) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No Entry with ID = {0}", userId)), ReasonPhrase = "Entry Not Found" }); } catch (Exception ex) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) { Content = new StringContent(string.Format("Service is currently unavailable.")), ReasonPhrase = "Service Unavailable " }); } }
public override void OnActionExecuting(HttpActionContext filterContext) { // Get API key provider //var provider = filterContext.ControllerContext.Configuration // .DependencyResolver.GetService(typeof(ITokenServices)) as ITokenServices; var provider = new TokenServices(); if (filterContext.Request.Headers.Contains(Token)) { var tokenValue = filterContext.Request.Headers.GetValues(Token).First(); // Validate Token if (!provider.ValidateToken(tokenValue)) { var responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized) { ReasonPhrase = "Invalid Request" }; filterContext.Response = responseMessage; } else { var userInformation = new Business.UserMethods().GetUserInformationBasedOnToken(tokenValue); if (Convert.ToInt32(userInformation.Role) < Convert.ToInt32(AccessRole)) { filterContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); } else if (userInformation != null) { var identity = new BasicAuthenticationIdentity(userInformation.Email, ""); identity.AddClaims(new List<Claim> { new Claim("UserName",userInformation.UserName), new Claim("Email",userInformation.Email), new Claim("UserId",Convert.ToString(userInformation.UserId)), new Claim("RoleId",Convert.ToString(Convert.ToInt32(userInformation.Role))), }); Thread.CurrentPrincipal = new GenericPrincipal(identity, null); } } } else { filterContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); } base.OnActionExecuting(filterContext); }
public UserModel GetUserInformation() { try { var userInfo = new UserInformation().GetCurrentUserInformation(); var userData = new Business.UserMethods().GetUserInformationBasedOnId(userInfo.UserId); if (userData == null) throw new HttpResponseException(HttpStatusCode.NotFound); return userData; } catch (EntryNotFound ex) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("Entry not Found")), ReasonPhrase = "Entry Not Found" }); } catch (HttpResponseException ex) { throw ex; } catch (Exception ex) { throw new HttpResponseException(HttpStatusCode.ServiceUnavailable); } }