// PUT: api/User/5 public void Put(int id, [FromBody] MdTblUser user) { using (ChatContextDataContext db = new ChatContextDataContext()) { var edit = (from a in db.TblUser where a.Id == id select a).FirstOrDefault(); edit.Name = user.Name; edit.Password = user.Password; edit.Photo = user.Photo; edit.Status = user.Status; db.SubmitChanges(); } }
public object Post([FromBody] MdTblUser value) { using (ChatContextDataContext db = new ChatContextDataContext()) { var existingUser = (from a in db.TblUser where a.Name == value.Name && a.Password == value.Password select new MdTblUser { Id = a.Id, Name = a.Name, Password = a.Password, Photo = a.Photo, Status = a.Status }).FirstOrDefault(); object dbUser; var token = Token.CreateToken(existingUser, out dbUser); var response = Request.CreateResponse(new { dbUser, token }); return(response); } }
public static string CreateToken(MdTblUser user, out object dbUser) { var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local); var expiry = Math.Round((DateTime.Now.AddMinutes(30) - unixEpoch).TotalSeconds); var issuedAt = Math.Round((DateTime.UtcNow - unixEpoch).TotalSeconds); var notBefore = Math.Round((DateTime.UtcNow.AddMonths(6) - unixEpoch).TotalSeconds); var payload = new Dictionary <string, object> { { "userId", user.Id }, { "name", user.Name }, { "expire", expiry } }; //var secret = ConfigurationManager.AppSettings.Get("jwtKey"); const string apikey = "tGv78M2ywDP2LJ3n3bWpqeG-N1VciK8YLpynB535QqOibS_bvZOVa_zVtpbrRjvb"; var token = JsonWebToken.Encode(payload, apikey, JwtHashAlgorithm.HS256); dbUser = new { user.Name, user.Id }; return(token); }