Esempio n. 1
0
        //Get the roles for displaying
        public List<RoleModel> GetRoles()
        {
            List<RoleModel> rolemodels = new List<RoleModel>();

            try
            {
                var roles = (SimpleRoleProvider)Roles.Provider;
                string[] UserRoles = roles.GetAllRoles();

                //If Roles alerady in the database
                if (UserRoles.Length > 0)
                {
                    //Add the role to RoleModel

                    for (int i = 0; i < UserRoles.Length; i++)
                    {
                        RoleModel role = new RoleModel();
                        role.RoleName = UserRoles[i];
                        rolemodels.Add(role);
                    }
                }
            }
            catch (Exception ex)
            {
                //write the log file for the exception
            }
            return rolemodels;
        }
Esempio n. 2
0
        public ActionResult GetRole(RoleModel model)
        {
            //Check if modelstate is valid
            if (ModelState.IsValid)
            {
                try
                {
                    RoleModel role = new RoleModel();
                    if (role.CreateRole(model.RoleName))
                    {
                        //Success for creating a new role
                      return   RedirectToAction("GetRole", "Account");
                    }
                    else
                    {
                        //Failed for creating a new role
                        ModelState.AddModelError("", "Sorry the  role alerady exists");
                    }
                }
                catch (Exception ex)
                {
                    //Exception for creating a new role
                    ModelState.AddModelError("", "Plese contact to support");
                }
            }

            return RedirectToAction("GetRole", "Account");
        }
Esempio n. 3
0
        public ActionResult GetRole()
        {
            //Get the all roles for creating a new user
            RoleModel role = new RoleModel();
            try
            {
                 var userroles = role.GetRoles();

                return View(userroles);
            }
            catch (Exception ex)
            {
                //Any Error on catch write a to a log file
                ModelState.AddModelError("", "Please contct to support help desk");
            }
            return View(role);
        }
Esempio n. 4
0
        public ActionResult Register(RegisterModel model,string Role)
        {
            //if model state is valid
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    //Adding a new user
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, false);
                    //Assign a role to user
                    Roles.AddUserToRole(model.UserName, Role);
                    //Set the cookies
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    //Redirect the user to Index method of Home Controller
                    return RedirectToAction("Index", "Home");
                }
                //If Fails
                catch (System.Web.Security.MembershipCreateUserException ex)
                {
                    //Please write the error in log file
                    RoleModel role = new RoleModel();

                    var userroles = role.GetRoles();

                    ViewData["UserRoles"] = userroles;
                    ModelState.AddModelError("", "Sorry the username alerady exists");
                }

            }
            else
            {
                //if model state is not valid then debug for the error
                var errors = ModelState
                    .Where(x => x.Value.Errors.Count > 0)
                    .Select(x => new { x.Key, x.Value.Errors })
                    .ToArray();
                return View(model);
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
Esempio n. 5
0
        public ActionResult Register()
        {
            //Get all the roles and put in viewdata for diplaying to user
            RoleModel role = new RoleModel();

            var userroles = role.GetRoles();

            ViewData["UserRoles"] = userroles;
            return View();
        }