public async Task <IActionResult> Put([FromBody] Authentication.Core.Users data) { try { var nd = db.Connection().getUserFromToken(TM.Core.HttpContext.Header("Authorization")); if (nd == null) { return(Json(new { msg = TM.Core.Common.Message.error_token.ToString() })); } var _data = await db.Connection().GetAsync <Authentication.Core.Users>(data.id); if (_data != null) { // _data.app_key = data.app_key; _data.full_name = data.full_name; _data.mobile = data.mobile; _data.email = data.email; _data.address = data.address; _data.descs = data.descs; _data.images = data.images; _data.donvi_id = data.donvi_id; _data.roles_id = data.roles_id; _data.updated_by = nd.ma_nd; _data.updated_at = DateTime.Now; } await db.Connection().UpdateAsync(_data); return(Json(new { data = _data, msg = TM.Core.Common.Message.success.ToString() })); } catch (System.Exception) { return(Json(new { msg = TM.Core.Common.Message.danger.ToString() })); } }
private string BuildToken(Authentication.Core.Users user) { var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_config["Jwt:Key"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( _config["Jwt:Issuer"], _config["Jwt:Issuer"], expires: DateTime.Now.AddMinutes(30), signingCredentials: creds); return(new JwtSecurityTokenHandler().WriteToken(token)); }
private List <Authentication.Core.RolesAcess> AuthRoles(Authentication.Core.Users user) { var rs = new List <Authentication.Core.RolesAcess>(); var roles = user.roles.Trim().Trim(',').Split(','); foreach (var i in roles) { rs.Add(new Authentication.Core.RolesAcess() { Controller = i, Action = "*" }); } return(rs); }
public async Task <IActionResult> Post([FromBody] Authentication.Core.Users data) { try { // var Authorization = TM.Core.HttpContext.Http.Request.Headers.TryGetValue("Authorization", out authorizationToken); ; // var Author = TM.Core.HttpContext.Http.Request.Headers["Author"].ToString(); var qry = $"select * from users where username='******'"; //AuthDB var user = await db.Connection().QueryFirstOrDefaultAsync <Authentication.Core.Users>(qry); //Account not Exist if (user == null) { return(Json(new { msg = TM.Core.Common.Message.exist.ToString() })); } //Password wrong data.password = TM.Core.Encrypt.MD5.CryptoMD5TM(data.password + user.salt); if (user.password != data.password) { return(Json(new { msg = TM.Core.Common.Message.wrong.ToString() })); } //Account is locked if (user.flag != 1) { return(Json(new { msg = TM.Core.Common.Message.locked.ToString() })); } // Roles qry = $"select * from user_role where user_id='{user.id}'"; var roles = await db.Connection().QueryAsync(qry); // Token var tokenString = BuildToken(user); //Update last login user.last_login = DateTime.Now; await db.Connection().UpdateAsync(user); return(Json(new { data = user, token = tokenString, roles = roles, msg = TM.Core.Common.Message.success.ToString() })); } catch (System.Exception) { return(Json(new { msg = TM.Core.Common.Message.danger.ToString() })); } }
public async Task <IActionResult> Post([FromBody] Authentication.Core.Users data) { try { var nd = db.Connection().getUserFromToken(TM.Core.HttpContext.Header("Authorization")); if (nd == null) { return(Json(new { msg = TM.Core.Common.Message.error_token.ToString() })); } if (db.Connection().isExist("users", "username", data.username)) { return(Json(new { msg = TM.Core.Common.Message.exist.ToString() })); } data.id = Guid.NewGuid().ToString("N"); data.created_by = nd.ma_nd; data.created_at = DateTime.Now; await db.Connection().InsertOraAsync(data); return(Json(new { data = data, msg = TM.Core.Common.Message.success.ToString() })); } catch (System.Exception) { return(Json(new { msg = TM.Core.Common.Message.danger.ToString() })); } }