public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { User user = null; using (UserContext db = new UserContext()) { user = db.Entities.FirstOrDefault(u => u.UserName == model.Name && u.Password == model.Password); } if (user == null) { // create user using (UserContext db = new UserContext()) { db.Entities.Add(new User { UserName = model.Name, Password = model.Password, Age = model.Age }); db.SaveChanges(); user = db.Entities.FirstOrDefault(u => u.UserName == model.Name && u.Password == model.Password); } // if added to db if (user != null) { FormsAuthentication.SetAuthCookie(model.Name, true); return RedirectToAction("Index", "Welcome"); } } else { ModelState.AddModelError("", "Name exists"); } } return View(model); }
public ActionResult Register(UserAccount account) { if(ModelState.IsValid) { using (UserContext db = new UserContext()) { db.userAccount.Add(account); db.SaveChanges(); } ModelState.Clear(); ViewBag.Message = account.userName + " registered"; } return View(); }
public ActionResult Login(UserAccount user) { using(UserContext db = new UserContext()) { var usr = db.userAccount.Where(u => u.userName == user.userName && u.userPw == user.userPw).FirstOrDefault(); if(usr != null) { Session["userId"] = usr.userId.ToString(); Session["userName"] = usr.userName.ToString(); return RedirectToAction("LoggedIn"); } else { ModelState.AddModelError("", "Username or Password incorrect"); } } return View(); }
public SimpleMembershipInitializer() { Database.SetInitializer<UserContext>(null); try { using (var context = new UserContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { // find user in db User user = null; using (UserContext db = new UserContext()) { user = db.Entities.FirstOrDefault(u => u.UserName == model.Name && u.Password == model.Password); } if (user != null) { FormsAuthentication.SetAuthCookie(model.Name, true); return RedirectToAction("Index", "Welcome"); } else { ModelState.AddModelError("", "Have no user with same login and password"); } } return View(model); }
public ActionResult CreateRoleSelectList(string id) { UserContext ctx = new UserContext(); var user = ctx.Users.Find(id); RolesDropDownViewModel vm = new RolesDropDownViewModel(); vm.SelectedRoleId = user.Roles.FirstOrDefault().RoleId; vm.Roles = new SelectList(ctx.Roles, "Id", "Name"); return PartialView("_ShowRoles", vm); }
public string RemoveUserRole(string id, string value) { UserContext ctx = new UserContext(); ApplicationUser user = ctx.Users.Find(id); //giver ikke mening endnu var role = user.Roles.Where(x => x.RoleId == value).Single(); user.Roles.Remove(role); ctx.SaveChanges(); return "done"; }
public string AddUserRole(string id, string value) { UserContext ctx = new UserContext(); ApplicationUser user = ctx.Users.Find(id); //giver ikke mening endnu IdentityUserRole role = new IdentityUserRole(); role.RoleId = value; role.UserId = id; user.Roles.Add(role); ctx.SaveChanges(); return "done"; }
public ActionResult ShowAllUsers() { UserContext ctx = new UserContext(); List<ApplicationUser> users = ctx.Users.ToList(); ViewBag.Roles = new SelectList(ctx.Roles, "Id", "Name"); return View(users); }
public ActionResult ReadUserNavigationDemo() { UserContext userContext = new UserContext(); string filePath = SysContext.Config.TempDirectory_Physical + SysContext.CommonService.CreateUniqueNameForFile("siteMapDemo.xml"); userContext.Navigation.RootNode.Save(filePath); return File(filePath, MimeHelper.GetMIMETypeForFile(".xml")); }
//*********The Following Method can be used if Authentication take place from custom users Table***********// public Boolean ValidateUser(string Username, string password, string UserLanguage, ref bool locked, ref string Msg) { Boolean status = false; using (BaseRepository baseRepo = new BaseRepository()) { // BizContext bc = new BizContext(); Business.BizTbl_User user = BizUser.GetUser(baseRepo.BizDB, string.Empty, Username, password); BizContext BizContext = new BizContext(); // BizContext bc = new BizContext(); UserContext uc = new UserContext(); // BizApplication.GetBizContext(Username, password, CultureCode,ref bc); if (user != null) { status = true; BizApplication.SetUserContext(baseRepo.BizDB, ref uc, Convert.ToInt64(user.ID), CultureCode); BizContext.UserContext = uc; //if (uc.IsHotelAdmin()) { // System.Linq.IQueryable<Business.TB_Hotel> userHotels = BizHotel.GetHotels(baseRepo.BizDB, null, uc.FirmID, null, user.ID.ToString()); // foreach (Business.TB_Hotel hotel in userHotels) // { // BizContext.Hotels.Add(hotel.ID, hotel.Name); // } // Business.TB_Hotel userHotel = userHotels.First(); // BizContext.HotelID = userHotel.ID; // BizContext.HotelCountryID = userHotel.CountryID; // BizContext.HotelRegionID = Convert.ToInt64(userHotel.RegionID); // BizContext.HotelCityID = Convert.ToInt64(userHotel.CityID); // BizContext.HotelCurrencyID = Convert.ToString(userHotel.CurrencyID); // // BizContext.HotelCurrencyName = dc.GetColumn(GetCurrencies(dc, CultureCode, bc.HotelCurrencyID)(0), "Name"); // BizContext.HotelAccommodationTypeID = userHotel.HotelAccommodationTypeID; // BizContext.HotelAvailabilityRateUpdate = Convert.ToBoolean(userHotel.AvailabilityRateUpdate); // BizContext.HotelRoutingName = userHotel.RoutingName; // BizContext.FirmID = Convert.ToString(userHotel.FirmID); // Session["SelectedHotelID"] = userHotel.ID; // Session["SelectedHotelName"] = userHotel.Name; //} if (BizContext.UserContext.IsHotelAdmin()) { // System.Linq.IQueryable<Business.TB_Hotel> userHotels = BizHotel.GetHotels(baseRepo.BizDB, null, uc.FirmID, null, user.ID.ToString()); int i = 0; baseRepo.SQLCon.Open(); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("B_Ex_GetUserHotelByUserID_TB_Hotel_SP", baseRepo.SQLCon); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@UserID", user.ID.ToString()); cmd.Parameters.AddWithValue("@FirmID", uc.FirmID); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); baseRepo.SQLCon.Close(); foreach (DataRow hotel in dt.Rows) { BizContext.Hotels.Add(Convert.ToInt32(hotel["ID"]), (hotel["Name"].ToString())); if (i == 0) { BizContext.HotelID = Convert.ToInt32(hotel["ID"]); BizContext.FirmID = hotel["FirmID"].ToString(); BizContext.HotelAccommodationTypeID = Convert.ToInt32(hotel["HotelAccommodationTypeID"]); BizContext.HotelRoutingName = hotel["Name"].ToString(); Session["SelectedHotelID"] = Convert.ToInt32(hotel["ID"]); Session["SelectedHotelName"] = hotel["Name"].ToString(); } i++; } } int userCountryID = 0; //if (bc.UserContext.IPAddress == string.Empty) //{ if (Session["GBAdminBizContext"] != null) { BizContext = (BizContext)Session["GBAdminBizContext"]; } Session["GBAdminBizContext"] = BizContext; if (UserLanguage != "") { try { string[] words = UserLanguage.Split(','); BizContext.SystemCultureCode = words[1]; BizContext.CultureCode = words[0]; Session["CultureCode"] = words[0]; Session["GBAdminBizContext"] = BizContext; } catch { BizContext.SystemCultureCode = "en-GB"; BizContext.CultureCode = "en"; Session["GBAdminBizContext"] = BizContext; Session["CultureCode"] = "en"; } } else { BizContext.SystemCultureCode = "en-GB"; BizContext.CultureCode = "en"; Session["GBAdminBizContext"] = BizContext; Session["CultureCode"] = "en"; } string userIpAddress = GetUserIPAddress(); //GetCultureByIpaddress(userIpAddress); try { CountriesRepository countryRepo = new CountriesRepository(); Business.TB_Country userCountryInfo = BizApplication.GetCountryInfoFromIPAddress(baseRepo.BizDB, userIpAddress); userCountryID = userCountryInfo.ID; } catch { userCountryID = 0; } // bc.UserContext.IPAddress = userIpAddress; // } //if (bc.UserSessionID == string.Empty) //{ AuthenticationRepository authRepo = new AuthenticationRepository(); string countryID = (userCountryID == 0 ? String.Empty : userCountryID.ToString()); string UserSessionID = BizUser.SaveUserSession(baseRepo.BizDB, String.Empty, Guid.NewGuid().ToString(), user.ID.ToString(), countryID, userIpAddress, DateTime.Now.ToString()).ToString(); //} if (Session["GBAdminBizContext"] != null) { BizContext = (BizContext)Session["GBAdminBizContext"]; } BizContext.UserSessionID = UserSessionID; Session["GBAdminBizContext"] = BizContext; // Session[BizCommon.AdminBizContextName] = bc; BizUser.AddUserOperation(baseRepo.BizDB, user.ID.ToString(), DateTime.Now.ToString(), BizCommon.Operation.Login, "", "", GetUserIPAddress(), UserSessionID); Session["username"] = user.DisplayName; Session["UserID"] = user.ID; } else { Msg = Resources.Resources.CheckYourUserNameAndPasswordWarning; //BizMessage.GetMessage(baseRepo.BizDB, "CheckYourUserNameAndPasswordWarning", "en"); } } return status; }