public object Post([FromBody] UserCredencialModel Body) { string Hash = Register.ComputeSha256Hah(Body.UserName, Body.Password, _config["SHA256:Salt"]); int UserID = _db.User.Where(u => u.UserName == Body.UserName && u.Password == Hash).Select(u => u.UserID).FirstOrDefault(); if (UserID != 0) { return(new { token = GenerateJSONWebToken(Body, UserID) }); } else { return(new { message = "not successful" }); } }
private string GenerateJSONWebToken(UserCredencialModel UserCred, int UserID) { SymmetricSecurityKey SecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"])); SigningCredentials credentials = new SigningCredentials(SecurityKey, SecurityAlgorithms.HmacSha256); Claim[] Claims = new Claim[] { new Claim(JwtRegisteredClaimNames.Sid, UserID.ToString()), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; var token = new JwtSecurityToken( _config["Jwt:Issuer"], _config["Jwt:Issuer"], Claims, expires: DateTime.Now.AddYears(10), signingCredentials: credentials); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public object Post([FromBody] UserCredencialModel Body) { if (ModelState.IsValid && _db.User.Where(u => u.UserName == Body.UserName).ToList().Count == 0) { List <UserNote> InitialNote = new List <UserNote> { new UserNote { Heading = "<h1>Heading</h1>", Note = "<p> </p>" } }; _db.User.Add(new User { UserName = Body.UserName, Password = ComputeSha256Hah(Body.UserName, Body.Password, _config["SHA256:Salt"]), UserNote = InitialNote }); _db.SaveChanges(); return(new { message = "successful" }); } else { return(new { message = "not successful" }); } }