Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
 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";
 }
Esempio n. 4
0
 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";
 }