public HttpResponseMessage <bool> Login(HttpRequestMessage request) { var connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString; var isOkay = Authenticator.Authenticate(connectionstring); if (isOkay) { //Guardo un nuevo Scoring ScoreRepository scoreRepository = new ScoreRepository(connectionstring); scoreRepository.AddLoginScore(System.Web.HttpContext.Current.User.Identity.Name); if (!Session.HasLoggedUser()) { EarthwatcherRepository ewRepo = new EarthwatcherRepository(connectionstring); var ew = ewRepo.GetEarthwatcher(System.Web.HttpContext.Current.User.Identity.Name, false); if (ew != null) { Session.GenerateCookie(ew); } } return(new HttpResponseMessage <bool>(isOkay) { StatusCode = HttpStatusCode.OK }); } else { return(new HttpResponseMessage <bool>(isOkay) { StatusCode = HttpStatusCode.Forbidden }); } }
public HttpResponseMessage <List <Rank> > GetContestLeaderBoard(int userid, HttpRequestMessage request) { try { IEarthwatcherRepository ewRepo = new EarthwatcherRepository(connectionstring); var earthwatcher = ewRepo.GetEarthwatcher(userid); var leaderBoard = scoreRepository.GetLeaderBoardNationalRanking(true, earthwatcher.PlayingRegion); var rankingCollection = leaderBoard.Take(10).ToList(); if (!rankingCollection.Any(x => x.EarthwatcherId == userid)) { rankingCollection.Add(leaderBoard.Where(x => x.EarthwatcherId == userid).First()); } foreach (var r in rankingCollection) { if (string.IsNullOrEmpty(r.Name)) { r.Name = r.Nick; } } return(new HttpResponseMessage <List <Rank> >(rankingCollection) { StatusCode = HttpStatusCode.OK }); } catch (Exception ex) { return(new HttpResponseMessage <List <Rank> >(null) { StatusCode = HttpStatusCode.InternalServerError, ReasonPhrase = ex.Message }); } }
public void GenerateAndUpdatePassword(Earthwatcher earthwatcher) { var prefix = PasswordChecker.GeneratePrefix(); var hashedPassword = PasswordChecker.GenerateHashedPassword(earthwatcher.Password, prefix); // store in database var connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString; var repos = new EarthwatcherRepository(connectionstring); repos.UpdatePassword(earthwatcher, prefix, hashedPassword); }
private static bool TryGetPrincipal(string connectionString, string userName, string password, out IPrincipal principal) { var earthwatcher = new EarthwatcherRepository(connectionString).GetEarthwatcher(userName, false); if (earthwatcher != null) { var membershipProvider = new EarthwatchersMembershipProvider(connectionString); var result = membershipProvider.ValidateUser(userName, password, earthwatcher.ApiEwId); if (result) { principal = new GenericPrincipal(new GenericIdentity(userName), earthwatcher.GetRoles()); return(true); } } principal = null; return(false); }
public static Earthwatcher CurrentUser() { if (!HttpContext.Current.Request.Headers.AllKeys.Contains("Authorization")) { return(null); } string authHeader = HttpContext.Current.Request.Headers["Authorization"]; var creds = ParseAuthHeader(authHeader); if (creds.Length < 2) { return(null); } EarthwatcherRepository repo = new EarthwatcherRepository(System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString); var user = repo.GetEarthwatcher(creds[0], false); return(user); }
public bool ValidateUser(string username, string password, int apiEwId = 0) { var isValid = false; ApiEw apiEw = null; EarthwatcherRepository earthwatcherRepo = new EarthwatcherRepository(connectionString); if (apiEwId != 0) { apiEw = earthwatcherRepo.GetApiEwById(apiEwId); } if (apiEw == null) { var credentials = CredentialsRepository.GetCredentials(connectionString, username); isValid = PasswordChecker.CheckPassword(password, credentials.Prefix, credentials.Hash); } else { //TODO: Validacion de AccessToken, por ahora si entro con una api lo manda derecho isValid = true; } return(isValid); }