Exemplo n.º 1
0
 public static List<MenuModel> GetMenuList()
 {
     using (DbContextFactory contextFactory = new DbContextFactory())
     {
         return contextFactory.Menus.Select(p=>p).ToList();
     }
 }
Exemplo n.º 2
0
        public ActionResult Users()
        {
            IEnumerable<UserProfile> users;
            using (DbContextFactory db = new DbContextFactory())
            {
                users = db.UserProfiles.ToList();
            }

            ViewBag.Roles = System.Web.Security.Roles.GetAllRoles();

            return View(users);
        }
Exemplo n.º 3
0
        public bool IsCurrentUserLocked()
        {
            var context = new DbContextFactory();
            var currentUser = context.UserProfiles.SingleOrDefault(p => p.UserId == GetCurrentUser());
            if (currentUser!=null)
            {
                if (currentUser.IsLocked)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            throw new NullReferenceException("not found current user");
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<DbContextFactory>(null);

                try
                {
                    using (var context = new DbContextFactory())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("GoerDataBase", "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);
                }
            }
Exemplo n.º 5
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    var context = new DbContextFactory();
                    Guid userKey = Guid.NewGuid();
                    string confirmToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                        propertyValues: new
                            {
                                Email = model.Email
                            }
                            , requireConfirmationToken:true
                        );

                    //var roles = (SimpleRoleProvider)System.Web.Security.Roles.Provider;
                    //var membership = (SimpleMembershipProvider)System.Web.Security.Membership.Provider;
                    //bool e=Roles.RoleExists("test1");
                    //System.Web.Security.Roles.CreateRole("test");
                    //var roles=Roles.GetAllRoles();
                    //Roles.AddUserToRole(model.UserName, Role.User.ToString());

                    //Is need the email validation when register
                    if (ConfigurationManager.AppSettings["EmailValidation"] != null)
                    {
                        bool emailValidation = bool.Parse(ConfigurationManager.AppSettings["EmailValidation"]);
                        if (emailValidation)
                        {
                            if (confirmToken != null)
                            {
                                var user = context.UserProfiles.SingleOrDefault(p => p.UserName == model.UserName);
                                if (user != null)
                                {
                                    if (SendConfirmEmail(user))
                                    {
                                        return RedirectToAction("EmailValidate", "Account");

                                    }

                                    return View("EmailValidate", user);
                                }

                            }

                            return RedirectToAction("Index", "Home"); ;
                        }
                    }

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

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

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 6
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (DbContextFactory db = new DbContextFactory())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }