// Update username public string UpdateUsername(UserOptionsRequest requestBody, string tokenString) { try { JwtSecurityToken token = new JwtSecurityToken(tokenString); string id = token.Claims.First(c => c.Type == "id").Value; // Get user by id from token User originalUser = _usersCollection.Find(a => a.id == id && a.Username == requestBody.Username).First(); if (requestBody.NewUsername == null || requestBody.Username == null || requestBody.Username != originalUser.Username) { throw new Exception(); } // Check if user with newUsername already exists if (!UserExists(requestBody.NewUsername, "1")) { return("User with same username already exists"); } FilterDefinition <User> filter = Builders <User> .Filter.Eq("username", requestBody.Username); UpdateDefinition <User> update = Builders <User> .Update.Set("username", requestBody.NewUsername); _usersCollection.FindOneAndUpdate <User>(filter, update); return("Username updated"); } catch (Exception) { return(null); } }
// Update user password public string UpdatePassword(UserOptionsRequest requestBody, string tokenString) { try { JwtSecurityToken token = new JwtSecurityToken(tokenString); string id = token.Claims.First(c => c.Type == "id").Value; // Get user by email from token User originalUser = _usersCollection.Find(a => a.id == id).First(); if (requestBody.NewPassword == null || requestBody.Password == null || !BC.Verify(requestBody.Password, originalUser.Password)) { throw new Exception(); } // Hash new Password string newPasswordHashed = BC.HashPassword(requestBody.NewPassword); FilterDefinition <User> filter = Builders <User> .Filter.Eq("username", originalUser.Username); UpdateDefinition <User> update = Builders <User> .Update.Set("password", newPasswordHashed); _usersCollection.FindOneAndUpdate <User>(filter, update); return("Password updated"); } catch (Exception) { return(null); } }
public ActionResult UpdateUsername([FromBody] UserOptionsRequest requestBody) { string message = _userContext.UpdateUsername(requestBody, HttpContext.Request.Headers["Authorization"]); if (message == null || message.Equals("User with same username already exists")) { return((message == null) ? BadRequest(new { error = "Wrong username" }) : StatusCode(409, new { message })); } return(Ok(new { message })); }
public ActionResult UpdatePassword([FromBody] UserOptionsRequest requestBody) { string message = _userContext.UpdatePassword(requestBody, HttpContext.Request.Headers["Authorization"]); if (message == null) { return(BadRequest(new { error = "Wrong password provided" })); } return(Ok(new { message })); }
public void GetUserOptionsSync(ILoginInfo loginInfo, Action <ConnectionSystemState, UserOptions> callback) { try { Hostname = loginInfo.Hostname; Port = loginInfo.Port; Security = loginInfo.Security; // create tcp client TcpMessageClient client = new TcpMessageClient(Hostname, Port, Security); client.StartReceiving(); UserOptionsRequest request = new UserOptionsRequest(callback); client.ReceivePipe = request; client.SendPipe.PostMessage(new Message(Symbol.Intern("UserOptions")).Append(Symbol.Intern("GetGroups"), Symbol.Intern("GetGroupIDs"))); } catch (Exception) // may be IO, may be other { callback(ConnectionSystemState.Unknown, new UserOptions()); } }
public string UpdateUsername(UserOptionsRequest requestBody, string token) { return(_service.UpdateUsername(requestBody, token)); }
public string UpdatePassword(UserOptionsRequest requestBody, string token) { return(_service.UpdatePassword(requestBody, token)); }