CreateRole() публичный Метод

public CreateRole ( string roleName ) : void
roleName string
Результат void
Пример #1
0
        public ActionResult AddRole(Role role)
        {
            CodeFirstRoleProvider roleProvider = new CodeFirstRoleProvider();

            roleProvider.CreateRole(role.RoleName);
            return(View("AdminTasks"));
        }
Пример #2
0
        public ActionResult ClientRegister(ClientRegister form)
        {
            ClientRegister details = new ClientRegister();

            details = form;
            int enqID = Convert.ToInt32(TempData["EnquiryIdClient"]);
            //RegisterModel regMod = new RegisterModel();
            //AccountController accController = new AccountController();
            Enquiry enq = new Enquiry();

            enq = enquiryRepository.Find(enqID);
            //regMod.UserName = enq.Contact.ContactEmail;
            string Password = PasswordGeneration(enq.Contact.ContactName, enq.Contact.MobileNumber);
            //regMod.Email = enq.Contact.ContactEmail;
            //accController.Register(regMod);
            MembershipCreateStatus      createStatus;
            CodeFirstMembershipProvider provider = new CodeFirstMembershipProvider();

            provider.CreateAccount(enq.Contact.ContactEmail, Password, enq.Contact.ContactEmail, out createStatus);
            CodeFirstRoleProvider roleProvider = new CodeFirstRoleProvider();

            if (createStatus == MembershipCreateStatus.Success)
            {
                if (!roleProvider.RoleExists("Client"))
                {
                    roleProvider.CreateRole("Client");
                }

                string[] roles = { "Client" };
                string[] users = { enq.Contact.ContactEmail.ToString() };
                roleProvider.AddUsersToRoles(users, roles);
            }
            details.EnquiryID = Convert.ToInt32(TempData["EnquiryIdClient"]);
            details.ContactID = enq.ContactID; // Please see this--Pankaj
            clientRegisterRepository.InsertOrUpdate(details);
            clientRegisterRepository.Save();
            return(RedirectToAction("Details", new { id = details.EnquiryID }));
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    var cfrp = new CodeFirstRoleProvider();

                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);

                    // By default add the user to the admin group, unless user has specified another group in the settings area
                    string defaultRole = "Administrators";
                    using (var context = new DataContext())
                    {
                        var siteSettings = context.SiteSettings.FirstOrDefault();
                        if (siteSettings != null && !String.IsNullOrEmpty(siteSettings.DefaultUserRole))
                        {
                            defaultRole = siteSettings.DefaultUserRole;
                        }
                    }

                    // Now check if user entered a registration Code.
                    // Registration Codes allow users to be inserted into a non-default role
                    if (!String.IsNullOrEmpty(model.RegistrationCode))
                    {
                        // Check for a Role that has this registration code
                        using (var context = new DataContext())
                        {
                            var role = context.Roles.FirstOrDefault(x => x.RegistrationCode == model.RegistrationCode);
                            if (role != null)
                            {
                                defaultRole = role.RoleName;
                            }
                        }
                    }

                    // Make sure roll exists
                    if (!Roles.RoleExists(defaultRole))
                    {
                        Roles.CreateRole(defaultRole);
                    }

                    // Double Check CodeFirst Provider
                    if (!cfrp.RoleExists(defaultRole))
                    {
                        cfrp.CreateRole(defaultRole);
                    }

                    // Sanity Check
                    if (!Roles.IsUserInRole(model.UserName, defaultRole))
                    {
                        Roles.AddUserToRole(model.UserName, defaultRole);
                    }
                    // Add to CodeFirst as well
                    cfrp.AddUsersToRoles(new string[] { model.UserName }, new string[] { defaultRole });

                    return RedirectToAction("Index", "Admin");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Пример #4
0
        protected override void Seed(BlogUnitOfWork context)
        {
            //Roles
            CodeFirstRoleProvider provider = new CodeFirstRoleProvider();

            provider.CreateRole("admin");
            provider.CreateRole("user");
            provider.CreateRole("premium");

            // Create indexes
            context.Database.ExecuteSqlCommand("CREATE INDEX IDX_Posts_Code ON Posts (Code);");
            context.Database.ExecuteSqlCommand("CREATE INDEX IDX_Posts_DateCreated ON Posts (DateCreated DESC);");
            context.Database.ExecuteSqlCommand("CREATE INDEX IDX_Subscriptions_SubscriberEmail ON Subscriptions (SubscriberEmail);");

            //Resources
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Admin_Role", Value = "admin"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Categories_Number_of_Categories_per_Page", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Comments_Number_of_Comments_per_Page", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Copyright", Value = "©MyCopyright 2XXX"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Dark_Background_Themes", Value = ""
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Dark_Header_Themes", Value = "Aristo;Rocket;Cobalt"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Default_Theme", Value = "Cobalt"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Directories_Temp_Data", Value = "~/Content/Files/Temp_Data/"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Email_Password", Value = "xxxxxx"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Email_UserName", Value = "*****@*****.**"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Google_Analytics_Track_Code", Value = "xx-xxxxxxxx-x"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Index_Number_of_Posts", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Logo", Value = "~/Content/Icons/logo.jpg"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "No_Image", Value = "~/Content/Icons/no_image.jpg"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Posts_Number_of_Posts_per_Page", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Premium_Role", Value = "premium"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Recaptcha_Private_Key_Http", Value = "xxxx"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Recaptcha_Public_Key_Http", Value = "xxxx"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Akismet_API_key", Value = ""
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Roles_Number_of_Roles_per_Page", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "SearchImages_Number_of_Images_per_Page", Value = "15"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "SearchVideos_Number_of_Videos_per_Page", Value = "12"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "SiteTitle", Value = "MySiteName.com"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "SiteUrl", Value = "http://www.MySiteDomain.com"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Smtp_Port", Value = "25"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Smtp_Server", Value = "smtp.live.com"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Email_SSL", Value = "false"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Send_Mail_When_Comment_Received", Value = "false"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Tags_Number_of_Tags_per_Page", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "ThumbnailHeight", Value = "150"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "ThumbnailWidth", Value = "200"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Users_Number_of_Users_per_Page", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Video_Container_Height", Value = "160"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Video_Container_Width", Value = "250"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Posts_HomeIndexPostsPerPage", Value = "10"
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Twitter_User", Value = ""
            });
            context.Set <BlogResource>().Add(new BlogResource {
                Name = "Twitter_Search_Query", Value = ""
            });
            context.SaveChanges();
        }