public IHttpActionResult Register(AccountModel user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } List <user> existsLoginOrMail = _db.user.Where(x => x.login == user.Login || x.mail == user.Mail).ToList(); if (existsLoginOrMail.Count > 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Login or Mail exists"))); } using (DbContextTransaction dbtransaction = _db.Database.BeginTransaction()) { try { user newUser = new user() { login = user.Login, password = Auth.HashPassword(user.Password), mail = user.Mail }; _db.user.Add(newUser); _db.SaveChanges(); roles roles = _db.roles.Where(x => x.name == "user").First(); userRoles newUserRoles = new userRoles() { userId = newUser.id, rolesId = roles.id }; _db.userRoles.Add(newUserRoles); _db.SaveChanges(); newUser.rolesId = newUserRoles.id; _db.user.Attach(newUser); _db.Entry(newUser).State = EntityState.Modified; _db.SaveChanges(); dbtransaction.Commit(); } catch { dbtransaction.Rollback(); return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Register user failed!"))); } return(Ok()); } }
public async Task <ActionResult> USERACCESS() { List <userRoles> userRolesList = new List <userRoles>(); using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["DatabaseServer"].ToString())) { if (con.State == System.Data.ConnectionState.Closed || con.State == System.Data.ConnectionState.Broken) { await con.OpenAsync(); } string SQLCMD = "exec dbo.COPR16_CUST_REPORT_USELIST"; using (var cmd = con.CreateCommand()) { cmd.CommandText = SQLCMD; DbDataReader reader = await cmd.ExecuteReaderAsync(); { //var model = Utils.Serialize((SqlDataReader)reader); while (reader.Read()) { userRoles data = new userRoles(); data.UserName = reader["UID"].ToString(); data.UserRoles = reader["ROLES"].ToString(); data.ExpireDateTime = reader["EXPIRE"] != DBNull.Value ? Convert.ToDateTime(reader["EXPIRE"]) : default(DateTime); data.NeverExpire = reader["NEVER_EXPIRE"] != DBNull.Value ? Convert.ToBoolean(reader["NEVER_EXPIRE"]) : default(bool); data.CreateDate = reader["CREATEDATE"] != DBNull.Value ? Convert.ToDateTime(reader["CREATEDATE"]): default(DateTime); data.CreateBy = reader["CREATEBY"].ToString(); data.UpdateDate = reader["UPDATEDATE"] != DBNull.Value ? Convert.ToDateTime(reader["UPDATEDATE"]) : default(DateTime); data.UpdateBy = reader["UPDATEBY"].ToString(); userRolesList.Add(data); } } } } return(View(userRolesList)); }