public User LoginFromWeb(CredentialsLite credentials) { if (credentials == null) throw new ParamMissingException("Missing credentials."); if (string.IsNullOrWhiteSpace(credentials.Password)) throw new InvalidValueException("Password cannot be empty."); if (string.IsNullOrEmpty(credentials.Email)) throw new InvalidValueException("Email cannot be empty."); // take hash of password before matching it in DB. //credentials.Password = credentials.Password; User user = Repository.Single<User>(x => x.Credential.Email.Equals(credentials.Email) && x.Credential.Password.Equals(credentials.Password) && x.Deleted != true, "Credential"); if (user == null) return null; else { // This is web login, lets not reset the token. //user.Credential.SecurityToken = Guid.NewGuid().ToString(); //user.Credential.WebSecurityToken = Guid.NewGuid().ToString(); user.Credential.LastModified = DateTime.UtcNow; user.Credential.TokenExpiry = DateTime.MaxValue; Repository.Save(); return user; } }
public User Login(CredentialsLite credentials) { if (credentials == null) throw new ParamMissingException("Missing credentials."); //if (string.IsNullOrEmpty(credentials.Email)) // throw new InvalidValueException("Email cannot be empty."); if (string.IsNullOrEmpty(credentials.Username) && string.IsNullOrWhiteSpace(credentials.Email)) { return null; } //throw new InvalidValueException("Username cannot be empty."); User user = null; if (string.IsNullOrWhiteSpace(credentials.Username) == false) { user = Repository.Single<User>(c => c.Deleted == false && c.Credential.Username.Equals(credentials.Username) && c.Credential.Password.Equals(credentials.Password), "Credential"); } else { user = Repository.Single<User>(c => c.Deleted == false && c.Credential.Email.Equals(credentials.Email) && c.Credential.Password.Equals(credentials.Password), "Credential"); } if (user == null) return null; else { // Regenrate the security token. user.Credential.SecurityToken = Guid.NewGuid().ToString(); user.Credential.LastModified = DateTime.UtcNow; user.Credential.TokenExpiry = DateTime.MaxValue; Repository.Save(); return user; } }
public string Login(string email, string password) { CredentialsLite userCredentials = new CredentialsLite(); userCredentials.Email = email; userCredentials.Password = Extensions.ExtensionMethods.ToSHA1EncodedString("" + password); User user = _userManager.LoginFromWeb(userCredentials); if (user == null) return null; WebCookieLite response = new WebCookieLite(user, Request.UrlReferrer); return JsonConvert.SerializeObject(response); }
public void CopyFromUserIncludeCredentials(User user) { CopyFromUser(user); if(user.Credential != null) Credentials = new CredentialsLite { Email = user.Credential.Email, Username = user.Credential.Username }; }
public HttpResponseMessage Login(CredentialsLite userCred) { try { if (userCred == null //|| ExtensionMethods.IsEmptyOrSpace(userCred.Email) || (ExtensionMethods.IsEmptyOrSpace(userCred.Username) && ExtensionMethods.IsEmptyOrSpace(userCred.Email)) || ExtensionMethods.IsEmptyOrSpace(userCred.Password)) return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param")); userCred.Password = ExtensionMethods.ToSHA1EncodedString(userCred.Password); //userCred.Email = userCred.Email.Trim().ToLower(); if(string.IsNullOrWhiteSpace(userCred.Username) == false) userCred.Username = userCred.Username.Trim().ToLower(); if (string.IsNullOrWhiteSpace(userCred.Email) == false) userCred.Email = userCred.Email.Trim().ToLower(); User user = _userManager.Login(userCred); if (user == null) return Request.CreateResponse(HttpStatusCode.Unauthorized, GetErrorResponse("Login failed.")); else { UserLite lite = new UserLite(); lite.CopyFromUserIncludeCredentials(user); UserCreationResponse result = new UserCreationResponse { UserId = user.UserId, Token = user.Credential.SecurityToken, User = lite }; return Request.CreateResponse(HttpStatusCode.OK, result); } } catch (ParamMissingException e){ return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message }); } catch (AlreadyExistsException e){ return Request.CreateResponse(HttpStatusCode.Conflict, new ErrorResponse{ Message = e.Message }); }catch (InvalidValueException e){ return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message }); }catch (UserNotFoundException e){ return Request.CreateResponse(HttpStatusCode.NotFound, new ErrorResponse{ Message = e.Message }); }catch (Exception e){ return Request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorResponse { Message = "Oops, server encountered an issue... " + e.Message }); } }