protected int permission(Controller c) { var authed = c.Request.IsAuthenticated; //this completely breaks when used in test controller. the controller being called doesn't have a Request instance. if (authed == true) { var username = c.User.Identity.Name; //Grab username from the cookie. using (var context = TARSUserDB) { var userInDB = context.TARSUserList .Where(u => u.userName == username) .FirstOrDefault(); if (userInDB == null) { TARSUser newuser = new TARSUser(); newuser.userName = username; newuser.permission = 1; TARSUserDB.TARSUserList.Add(newuser); TARSUserDB.SaveChanges(); return 1; } else { return userInDB.permission; } } } return 0; }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { TARSUser newuser = new TARSUser(); newuser.userName = model.UserName; newuser.permission = 1; TARSUserDBContext user = new TARSUserDBContext(); user.TARSUserList.Add(newuser); user.SaveChanges(); FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed, redisplay form return View(model); }