public Claim Login(LoginRequest loginModel) { if (loginModel == null) { throw new SSOBaseException("Login Model required.", HttpStatusCode.BadRequest); } var user = _db.Users.SingleOrDefault(u => u.Username.Equals(loginModel.Username)); if (user == null) { throw new WrongCredentialsException(); } byte[] saltB = Convert.FromBase64String(user.Salt); byte[] passwordB = System.Text.Encoding.UTF8.GetBytes(loginModel.Password); var hashAlgorithm = new System.Security.Cryptography.SHA256Cng(); byte[] passwordHashB = hashAlgorithm.ComputeHash(passwordB.Concat(saltB).ToArray()); var passwordHashS = Convert.ToBase64String(passwordHashB); // TODO change database password field to nvarchar // 44 is length of if (!passwordHashS.Equals(user.Password.Substring(0, 44))) { throw new WrongCredentialsException(); } // Succeful login // Make token var rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); byte[] tokenB = new byte[40]; rng.GetBytes(tokenB); //Convert to hex String tokenHex = BitConverter.ToString(tokenB).Replace("-", String.Empty); Claim claim = new Claim(); claim.Token = tokenHex; claim.Valid = "1"; claim.Created = DateTime.Now; claim.User = user; _db.Claims.Add(claim); _db.SaveChanges(); return(claim); }
/// <summary> /// Method to set Seeds into RolePermissions-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <RolePermission> seedList = new List <RolePermission> { new RolePermission { RoleId = GetId.RoleID("admin", db), PermissionId = GetId.PermissionID("Root", db) }, new RolePermission { RoleId = GetId.RoleID("admin", db), PermissionId = GetId.PermissionID("Edit plan", db) }, new RolePermission { RoleId = GetId.RoleID("dispatcher", db), PermissionId = GetId.PermissionID("Edit plan", db) }, new RolePermission { RoleId = GetId.RoleID("dispatcher", db), PermissionId = GetId.PermissionID("Edit arrival", db) }, new RolePermission { RoleId = GetId.RoleID("dispatcher", db), PermissionId = GetId.PermissionID("Edit departure", db) } }; foreach (var item in seedList) { db.RolePermissions.Add(item); } db.SaveChanges(); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { try { string sessionid = ""; try { sessionid = filterContext.HttpContext.Request.Cookies["session"].Values["sid"]; } catch (Exception) { throw new Exception("用户尚未登录"); } using (var ctx = new SSOContext()) { var session = ctx.Sessions.Where(m => m.SessionId == sessionid).FirstOrDefault(); if (session == null) { throw new Exception("非法参数"); } if (session != null && session.ExpireTime.CompareTo(DateTime.Now) > 0) { session.ExpireTime = DateTime.Now.AddHours(1); HttpCookie cookie = new HttpCookie("session"); cookie.Expires = session.ExpireTime; cookie.Values.Add("sid", session.SessionId); cookie.Values.Add("uid", session.UserId); cookie.Values.Add("ctime", session.CreateTime.ToString()); cookie.Values.Add("etime", session.ExpireTime.ToString()); filterContext.HttpContext.Response.SetCookie(cookie); ctx.SaveChanges(); } if (session != null && session.ExpireTime.CompareTo(DateTime.Now) <= 0) { ctx.Sessions.Remove(session); ctx.SaveChanges(); throw new Exception("会话过期,请重新登录。"); } } } catch (Exception e) { filterContext.Result = new RedirectResult("/api/Home/ValidFailed?exception=" + e.Message); } }
/// <summary> /// Method to set Seeds into CompanyAirportsSet-Table. /// </summary> /// <param name="db">DB context.</param> public static void SetSeeds(SSOContext db) { List <CompanyAirports> seedList = new List <CompanyAirports> { new CompanyAirports { CompanyId = GetId.CompanyID("PROFI-IT", db), AirportId = 1 }, new CompanyAirports { CompanyId = GetId.CompanyID("ODS", db), AirportId = 1 } }; db.AddRange(seedList); db.SaveChanges(); }
public string Logout() { try { using (var ctx = new SSOContext()) { #region 登录过的,删除数据库和客户端cookie。 string sessionid = ""; try { sessionid = Request.Cookies["session"].Values["sid"]; } catch (Exception) { } var session = ctx.Sessions.Where(m => m.SessionId == sessionid).FirstOrDefault(); if (session != null) { ctx.Sessions.Remove(session); ctx.SaveChanges(); } #region cookie重置 HttpCookie cookie = new HttpCookie("session"); cookie.Expires = DateTime.Now; cookie.Values.Add("sid", "0"); cookie.Values.Add("uid", "0"); cookie.Values.Add("ctime", DateTime.Now.ToString()); cookie.Values.Add("etime", DateTime.Now.ToString()); Response.SetCookie(cookie); #endregion #endregion return(JsonConvert.SerializeObject(new { res = "OK", msg = "成功退出当前账号" })); } } catch (Exception e) { return(JsonConvert.SerializeObject(new { res = "ERROR", msg = e.Message })); } }
public string UpdateUserInfo() { try { using (var ctx = new SSOContext()) { var sessionid = Request.Cookies["session"].Values["sid"]; var session = ctx.Sessions.Where(m => m.SessionId == sessionid).FirstOrDefault(); if (session != null) { var user = ctx.Users.Where(m => m.UserId == session.UserId).FirstOrDefault(); if (user != null) { //用户id,登录名,密码不在此处修改 user.NickName = Request.Form["NickName"]; user.Profile = Request.Form["Profile"]; user.Gender = Request.Form["Gender"]; user.Birthday = DateTime.Parse(Request.Form["Birthday"]).ToString("yyyy-MM-dd"); user.Location = Request.Form["Location"]; user.Phone = Request.Form["Phone"]; user.Email = Request.Form["Email"]; ctx.SaveChanges(); return(JsonConvert.SerializeObject(new { res = "OK", msg = "用户信息修改成功" })); } } return(JsonConvert.SerializeObject(new { res = "ERROR", msg = "登录信息过期或找不到当前用户" })); } } catch (Exception e) { return(JsonConvert.SerializeObject(new { res = "ERROR", msg = e.Message })); } }
/// <summary> /// Method to set Seeds into UserRoles-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <UserRole> seedList = new List <UserRole> { new UserRole { RoleId = GetId.RoleID("admin", db), UserId = GetId.UserID("greentee5", db) }, new UserRole { RoleId = GetId.RoleID("admin", db), UserId = GetId.UserID("sipdrenma", db) } }; foreach (var item in seedList) { db.UserRoles.Add(item); } db.SaveChanges(); }
public ActionResult ChangePassword(ChangePasswordRequest pwModel) { var selectedUser = _db.Users.SingleOrDefault(u => u.ID == pwModel.ID); if (selectedUser == null) { throw new SSOBaseException("User resource doesn't exist.", System.Net.HttpStatusCode.NoContent); } string freshSalt = CryptoHelper.generateSalt(); selectedUser.Salt = freshSalt; selectedUser.Password = CryptoHelper.generateHash(freshSalt, pwModel.NewPassword); _db.SaveChanges(); return(new ActionResult { Message = "Password successfully changed." }); }
/// <summary> /// Method to set Seeds into Companies-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <Company> seedList = new List <Company> { new Company { CompanyName = "PROFI-IT", Slug = "" }, new Company { CompanyName = "ODS", Slug = "" } }; foreach (var item in seedList) { item.Slug = item.CompanyName.Replace(' ', '-').ToLower(); db.Companies.Add(item); } db.SaveChanges(); }
/// <summary> /// Method to set Seeds into Roles-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <Role> seedList = new List <Role> { new Role { Slug = "admin", CompanyId = GetId.CompanyID("PROFI-IT", db) }, new Role { Slug = "dispatcher", CompanyId = GetId.CompanyID("ODS", db) } }; foreach (var item in seedList) { item.Slug = item.Slug.Replace(' ', '-').ToLower(); db.Roles.Add(item); } db.SaveChanges(); }
/// <summary> /// Method to set Seeds into UserParameters-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <UserParams> seedList = new List <UserParams> { new UserParams { UserId = GetId.UserID("greentee5", db), Key = "phone", Value = "+380501689853" }, new UserParams { UserId = GetId.UserID("greentee5", db), Key = "email", Value = "*****@*****.**" }, new UserParams { UserId = GetId.UserID("sipdrenma", db), Key = "email", Value = "*****@*****.**" } }; foreach (var item in seedList) { db.UserParameters.Add(item); } db.SaveChanges(); }
/// <summary> /// Method to set Seeds into Users-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <User> seedList = new List <User> { new User { Name = "greentee5", FirstName = "Yan", LastName = "Kolovorotny", Email = "*****@*****.**", Password = BCrypt.Net.BCrypt.HashPassword("zev1982z", salt), PhoneNumber = "+380501689853", CompanyId = GetId.CompanyID("PROFI-IT", db) }, new User { Name = "sipdrenma", FirstName = "Piter", LastName = "Parker", Email = "*****@*****.**", Password = BCrypt.Net.BCrypt.HashPassword("uncleBen", salt), PhoneNumber = "+3808008808", CompanyId = GetId.CompanyID("PROFI-IT", db) } }; foreach (var item in seedList) { db.Users.Add(item); } db.SaveChanges(); }
public string DelUser() { try { using (var ctx = new SSOContext()) { var sessionid = Request.Cookies["session"].Values["sid"]; var session = ctx.Sessions.Where(m => m.SessionId == sessionid).FirstOrDefault(); if (session != null) { var user = ctx.Users.Where(m => m.UserId == session.UserId).FirstOrDefault(); if (user != null) { ctx.Users.Remove(user); ctx.SaveChanges(); return(JsonConvert.SerializeObject(new { res = "OK", msg = "当前用户已销号" })); } } return(JsonConvert.SerializeObject(new { res = "ERROR", msg = "登录信息过期或找不到当前用户" })); } } catch (Exception e) { return(JsonConvert.SerializeObject(new { res = "ERROR", msg = e.Message })); } }
public string Register() { try { using (var ctx = new SSOContext()) { var ent = new User() { LoginName = Request.Form["LoginName"], Password = Request.Form["Password"], NickName = Request.Form["NickName"], Profile = Request.Form["Profile"], Gender = Request.Form["Gender"], Birthday = DateTime.Parse(Request.Form["Birthday"]).ToString("yyyy-MM-dd"), Location = Request.Form["Location"], Phone = Request.Form["Phone"], Email = Request.Form["Email"], }; ctx.Users.Add(ent); ctx.SaveChanges(); return(JsonConvert.SerializeObject(new { res = "OK", msg = "注册成功" })); } } catch (Exception e) { return(JsonConvert.SerializeObject(new { res = "ERROR", msg = e.Message })); } }
public string Login() { try { using (var ctx = new SSOContext()) { #region 判断是否已经登录,登录过的有效的重定向,无效的继续。 { string sessionid = ""; try { sessionid = Request.Cookies["session"].Values["sid"]; } catch (Exception) { } var session = ctx.Sessions.Where(m => m.SessionId == sessionid).FirstOrDefault(); //有效的已登录信息 if (session != null && session.ExpireTime.CompareTo(DateTime.Now) > 0) { return(JsonConvert.SerializeObject(new { res = "OK", msg = "已验证用户" })); } //无效的删除数据库记录 if (session != null && session.ExpireTime.CompareTo(DateTime.Now) <= 0) { ctx.Sessions.Remove(session); ctx.SaveChanges(); } } #endregion #region 重新登录进行身份验证 var username = Request.Form["username"]; var password = Request.Form["password"]; var user = ctx.Users.Where(m => m.LoginName == username).FirstOrDefault(); if (user != null && user.Password == password) { var session = new Session() { SessionId = Guid.NewGuid().ToString(), UserId = user.UserId, CreateTime = DateTime.Now, ExpireTime = DateTime.Now.AddHours(1) }; //新增前删除所有相关信息 ctx.Sessions.RemoveRange(ctx.Sessions.Where(m => m.UserId == user.UserId)); ctx.Sessions.Add(session); ctx.SaveChanges(); #region cookie设置 HttpCookie cookie = new HttpCookie("session"); cookie.Expires = session.ExpireTime; cookie.Values.Add("sid", session.SessionId); cookie.Values.Add("uid", session.UserId); cookie.Values.Add("ctime", session.CreateTime.ToString()); cookie.Values.Add("etime", session.ExpireTime.ToString()); Response.SetCookie(cookie); #endregion return(JsonConvert.SerializeObject(new { res = "OK", msg = "登录成功" })); } else { throw new Exception("请检查用户名和密码"); } #endregion } } catch (Exception e) { return(JsonConvert.SerializeObject(new { res = "ERROR", msg = e.Message })); } }
/// <summary> /// Method to set Seeds into Permissions-Table. /// </summary> /// <param name="db"><see cref="DBConfig"/> object.</param> public static void SetSeeds(SSOContext db) { List <Permission> seedList = new List <Permission> { new Permission { Slug = "set_permissions_to_role", Table = "common" }, new Permission { Slug = "set_roles_to_user", Table = "common" }, new Permission { Slug = "edit_roles", Table = "common" }, new Permission { Slug = "edit_permissions", Table = "common" }, new Permission { Slug = "edit_companies", Table = "common" }, new Permission { Slug = "edit_user_params", Table = "common" }, new Permission { Slug = "edit_user", Table = "common" }, new Permission { Slug = "Root", Table = "common" }, new Permission { Slug = "View logs", Table = "common" }, new Permission { Slug = "Edit groups", Table = "common" }, new Permission { Slug = "Edit users", Table = "common" }, new Permission { Slug = "Edit services", Table = "common" }, new Permission { Slug = "Edit airline", Table = "common" }, new Permission { Slug = "Edit airport", Table = "common" }, new Permission { Slug = "Edit arrival", Table = "common" }, new Permission { Slug = "Edit departure", Table = "common" }, new Permission { Slug = "Edit code of delay", Table = "common" }, new Permission { Slug = "Edit screenplay", Table = "common" }, new Permission { Slug = "Edit registration", Table = "common" }, new Permission { Slug = "Edit slide", Table = "common" }, new Permission { Slug = "Edit aircraft", Table = "common" }, new Permission { Slug = "Edit flight", Table = "common" }, new Permission { Slug = "Edit plan", Table = "common" }, new Permission { Slug = "Edit cancel", Table = "common" }, new Permission { Slug = "Edit gate", Table = "common" }, new Permission { Slug = "Edit profile", Table = "common" }, new Permission { Slug = "Edit cargo", Table = "common" }, new Permission { Slug = "Edit past days flight records", Table = "common" }, // new Permission { Slug = "enabled", Table = "arrivals" }, //new Permission {Slug = "cancelled",Table = "arrivals"}, new Permission { Slug = "Airport (not air hub)", Table = "arrivals" }, new Permission { Slug = "Airport Terminal", Table = "arrivals" }, new Permission { Slug = "Air hub (not airport)", Table = "arrivals" }, new Permission { Slug = "Flight status", Table = "arrivals" }, new Permission { Slug = "Flight state", Table = "arrivals" }, new Permission { Slug = "Flight plan", Table = "arrivals" }, new Permission { Slug = "Aircraft", Table = "arrivals" }, new Permission { Slug = "Airline", Table = "arrivals" }, new Permission { Slug = "Airport stand", Table = "arrivals" }, new Permission { Slug = "Registration number", Table = "arrivals" }, new Permission { Slug = "Aircraft layout", Table = "arrivals" }, new Permission { Slug = "Schedule Time Departure", Table = "arrivals" }, new Permission { Slug = "Actual Time Departure", Table = "arrivals" }, new Permission { Slug = "New Time Departure", Table = "arrivals" }, new Permission { Slug = "Schedule Time Arrival", Table = "arrivals" }, new Permission { Slug = "Estimated Time Arrival", Table = "arrivals" }, new Permission { Slug = "New Estimated Time Arrival", Table = "arrivals" }, new Permission { Slug = "Actual Time Arrival", Table = "arrivals" }, new Permission { Slug = "Chocks On", Table = "arrivals" }, new Permission { Slug = "Duplicate from cancelled", Table = "arrivals" }, new Permission { Slug = "Economy Class", Table = "arrivals" }, new Permission { Slug = "Business Class", Table = "arrivals" }, new Permission { Slug = "Special Class", Table = "arrivals" }, new Permission { Slug = "Transit Passengers", Table = "arrivals" }, new Permission { Slug = "Unaccompanied Minors", Table = "arrivals" }, new Permission { Slug = "Property Irregularity Report", Table = "arrivals" }, new Permission { Slug = "Baggage", Table = "arrivals" }, new Permission { Slug = "Cargo", Table = "arrivals" }, new Permission { Slug = "Mail", Table = "arrivals" }, new Permission { Slug = "Service Information Message", Table = "arrivals" }, new Permission { Slug = "Luggage on the tape", Table = "arrivals" }, new Permission { Slug = "Luggage tapes", Table = "arrivals" }, // new Permission { Slug = "enabled", Table = "departures" }, //new Permission {Slug = "cancelled",Table = "departures"}, new Permission { Slug = "Airport (not air hub)", Table = "departures" }, new Permission { Slug = "Airport Terminal", Table = "departures" }, new Permission { Slug = "Air hub (not airport)", Table = "departures" }, new Permission { Slug = "Flight status", Table = "departures" }, new Permission { Slug = "Flight state", Table = "departures" }, new Permission { Slug = "Flight plan", Table = "departures" }, new Permission { Slug = "Aircraft", Table = "departures" }, new Permission { Slug = "Airline", Table = "departures" }, new Permission { Slug = "Airport stand", Table = "departures" }, new Permission { Slug = "Registration number", Table = "departures" }, new Permission { Slug = "Aircraft layout", Table = "departures" }, new Permission { Slug = "Schedule Time Departure", Table = "departures" }, new Permission { Slug = "Actual Time Departure", Table = "departures" }, new Permission { Slug = "New Time Departure", Table = "departures" }, new Permission { Slug = "Schedule Time Arrival", Table = "departures" }, new Permission { Slug = "Schedule Start Check In", Table = "departures" }, new Permission { Slug = "Start Check In", Table = "departures" }, new Permission { Slug = "End Check In", Table = "departures" }, new Permission { Slug = "Aircraft Ready", Table = "departures" }, new Permission { Slug = "Boarding start", Table = "departures" }, new Permission { Slug = "Boarding closed", Table = "departures" }, new Permission { Slug = "Loading Baggage Closed", Table = "departures" }, new Permission { Slug = "Trap Cleaning Time", Table = "departures" }, new Permission { Slug = "Last Passenger Time", Table = "departures" }, new Permission { Slug = "Chocks Off", Table = "departures" }, new Permission { Slug = "Take Off Time", Table = "departures" }, new Permission { Slug = "Time Delay", Table = "departures" }, new Permission { Slug = "Duplicate from cancelled", Table = "departures" }, new Permission { Slug = "Passenger Preloading", Table = "departures" }, new Permission { Slug = "Economy Class", Table = "departures" }, new Permission { Slug = "Business Class", Table = "departures" }, new Permission { Slug = "Special Class", Table = "departures" }, new Permission { Slug = "Transit Passengers", Table = "departures" }, new Permission { Slug = "Unaccompanied Minors", Table = "departures" }, new Permission { Slug = "Passengers To Pay", Table = "departures" }, new Permission { Slug = "Infant Passengers", Table = "departures" }, new Permission { Slug = "Staff Passengers", Table = "departures" }, new Permission { Slug = "Depot Passengers", Table = "departures" }, new Permission { Slug = "Other Passengers", Table = "departures" }, new Permission { Slug = "Baggage", Table = "departures" }, new Permission { Slug = "Cargo", Table = "departures" }, new Permission { Slug = "Mail", Table = "departures" }, new Permission { Slug = "Service Information Message", Table = "departures" }, new Permission { Slug = "If code delay issue is solved", Table = "departures" } }; foreach (var item in seedList) { item.Slug = item.Slug.Replace(' ', '-').ToLower(); db.Permissions.Add(item); } db.SaveChanges(); }