Esempio n. 1
0
        protected override void Seed(MPSContext context)
        {
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));
            var userManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));

            //uzytkownicy o roli Admin
            roleManager.Create(new IdentityRole("Admin"));
            var u1 = new ApplicationUser
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };
            string passwor = "Biblioteka1@";

            userManager.Create(u1, passwor);
            userManager.AddToRole(u1.Id, "Admin");

            var uu1 = new UserNew
            {
                UserName = "******",
                Id       = u1.Id
            };

            context.User.Add(uu1);
            context.SaveChanges();

            /*   var P = new Person
             * {
             *     Name = "",
             *     Surname = ""
             *
             * };*/
            // context.Person.Add(P);
            //   context.SaveChanges();
        }
Esempio n. 2
0
        public ActionResult CreateEmployee(FormCollection form)
        {
            bool isAvailable = true;

            ViewBag.error = "";
            var    UserManager = new Microsoft.AspNet.Identity.UserManager <AspNetUser>(new UserStore <AspNetUser>(context));
            string UserName    = form["txtEmail"];
            string email       = form["txtEmail"];
            string pwd         = "Caretta.97";

            List <AspNetUser> uList = db.Users.ToList();

            foreach (var item in uList)
            {
                if (item.Email == email)
                {
                    ViewBag.error = "The employee you want to add is already registered.";
                    isAvailable   = false;
                }
            }
            if (isAvailable)
            {
                var user = new AspNetUser();
                user.UserName   = UserName;
                user.Email      = email;
                user.pwdChanged = false;
                var newUser = UserManager.Create(user, pwd);
                UserManager.AddToRole(user.Id, "personel");
                ViewBag.error = "The employee you want to add has been successfully registered.";
            }

            return(View());
        }
Esempio n. 3
0
        public ActionResult Register(RegisterModel model, HttpPostedFileBase file)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    var path     = Server.MapPath("~/Upload/Users"); //dosya yolu
                    var filename = Path.ChangeExtension(model.Username + "", ".jpg");

                    if (file == null)
                    {
                        filename = "place.jpg";
                    }
                    var fullpath = Path.Combine(path, filename);

                    file.SaveAs(fullpath);

                    model.UserImage = filename.ToString();

                    //Kayıt İşlemleri

                    ApplicationUser user = new ApplicationUser();

                    user.Name     = model.Name;
                    user.Surname  = model.Surname;
                    user.Email    = model.Email;
                    user.UserName = model.Username;

                    IdentityResult result = userManager.Create(user, model.Password);

                    if (result.Succeeded)
                    {
                        //Kullanıcı oluştu ve rol atanabilir.


                        if (roleManager.RoleExists("user"))
                        {
                            userManager.AddToRole(user.Id, "user");
                        }

                        return(RedirectToAction("Login", "Account"));
                    }
                    else
                    {
                        ModelState.AddModelError("RegisterUserError", "Kullanıcı oluşturulamadı.");
                    }
                }



                return(View(model));
            }
        }
Esempio n. 4
0
        protected override void Seed(ThingWall.Data.DataContext context)
        {
            //PROTIP: tutaj mo¿na wype³niæ bazê danych wstêpnymi danymi
            //UWAGA: trzeba sprawdzaæ, czy dany rekord ju¿ istnieje, Seed uruchamiany jest po ka¿dej migracji

            var examples = new List <ExampleItem>()
            {
                new ExampleItem()
                {
                    Name = "Test 1"
                },
                new ExampleItem()
                {
                    Name = "Test 2"
                },
                new ExampleItem()
                {
                    Name = "Test 3"
                }
            };

            //PROTIP: mechanizm u¿ytkowników dostarza ASP.NET Identity
            //klasa UserManager pozwala trochê ³atwiej ich ogarn¹æ
            var userManager = new Microsoft.AspNet.Identity.UserManager <User>(new UserStore <User>(context));

            var testUser = userManager.FindByEmail("*****@*****.**");

            if (testUser == null)
            {
                var hasher = new PasswordHasher();

                testUser = new User()
                {
                    UserName       = "******",
                    Email          = "*****@*****.**",
                    EmailConfirmed = true,
                    PasswordHash   = hasher.HashPassword("test"),
                    Nick           = "NowyNick"
                };

                userManager.Create(testUser);
            }

            foreach (var example in examples)
            {
                if (!context.ExampleItems.Any(i => i.Name == example.Name))
                {
                    //PROTIP: ustawianie klucza obcego wystarczy do "zrobienia" relacji
                    example.OwnerId = testUser.Id;
                    context.ExampleItems.Add(example);
                }
            }

            //PROTIP: SaveChanges poza pêtl¹ wrzuca wszystkie zmiany w kolejce jednym zapytaniem
            context.SaveChanges();
        }
Esempio n. 5
0
        public void CreateAdmin()
        {
            var userManger = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db));
            var user       = new ApplicationUser();

            user.Email    = "*****@*****.**";
            user.UserName = "******";
            var isUserExist = userManger.Create(user, "123456");

            if (isUserExist.Succeeded)
            {
                userManger.AddToRole(user.Id, "Admin");
            }
        }
        /// <summary>
        /// Step 1 : user creation
        /// </summary>
        /// <param name="model">Registration details</param>
        /// <returns></returns>
        public int RegisterUser(ParticipantRegistration model)
        {
            string createdBy     = string.Empty;
            int    userDetailsID = 0;

            #region Create useer
            using (OCASIAMeetingUOW db = new OCASIAMeetingUOW())
            {
                var res = db.Repository <ApplicationUser>().GetAll().Where(f => f.Email == model.Email).FirstOrDefault();
                if (res == null)
                {
                    var user = new ApplicationUser()
                    {
                        UserName         = model.Email,
                        Email            = model.Email,
                        RoleCustomID     = 3,
                        IsActive         = true,
                        IspasswordActive = true
                    };
                    var userManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore <ApplicationUser>(new OCASIAMeetingContext()));
                    var ures        = userManager.Create(user, model.Password);
                    if (ures.Succeeded)
                    {
                        // userManager.AddClaim(user.Id, new System.Security.Claims.Claim("FullName", model.Email));
                        Passwordhash mdl = new Passwordhash()
                        {
                            UserId   = user.Id,
                            Password = model.Password
                        };
                        db.Repository <Passwordhash>().Add(mdl);
                        db.SaveChanges();
                        createdBy = mdl.UserId;
                    }
                }
                else
                {
                    return(-1);
                }
            }
            #endregion
            if (string.IsNullOrEmpty(createdBy))
            {
                return(userDetailsID);
            }
            userDetailsID = CreateUserDetails(model, createdBy);
            return(userDetailsID);
        }
Esempio n. 7
0
        private void CreateRolesAndUsers()
        {
            ApplicationDbContext context = new ApplicationDbContext();

            var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var UserManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            if (!roleManager.RoleExists("Admin"))
            {
                // first we create Admin rool
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "Admin";
                roleManager.Create(role);

                var user = new ApplicationUser();
                user.UserName = "******";
                user.Email    = "*****@*****.**";

                string userPWD = "HuaTheDogN0se!";

                var chkUser = UserManager.Create(user, userPWD);


                if (chkUser.Succeeded)
                {
                    var result1 = UserManager.AddToRole(user.Id, "Admin");
                }
            }

            // creating Creating Manager role
            if (!roleManager.RoleExists("Employee"))
            {
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "Employee";
                roleManager.Create(role);
            }

            // creating Creating Employee role
            if (!roleManager.RoleExists("Customer"))
            {
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "Customer";
                roleManager.Create(role);
            }
        }
        protected override void  Seed(TrainerClientADOnet.Models.ApplicationDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.


            if (!context.Roles.Any(r => r.Name == "Admin"))
            {
                var store   = new RoleStore <IdentityRole>(context);
                var manager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(store);
                var role    = new IdentityRole {
                    Name = "Admin"
                };

                manager.Create(role);
            }



            if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
            {
                var store   = new UserStore <ApplicationUser>(context);
                var manager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(store);
                var user    = new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**"
                };

                manager.Create(user, "Teodora!1");
                context.Users.Add(user);
                manager.AddToRole(user.Id, "Admin");
            }


            //   if (!context.Roles.Any(u => u.Name == "Trener"))
            //   {
            if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
            {
                var store   = new UserStore <ApplicationUser>(context);
                var manager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(store);
                var user    = new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**"
                };

                manager.Create(user, "Teodora!1");
                context.Users.Add(user);
                manager.AddToRole(user.Id, "Trener");
            }
            //  }



            if (!context.Roles.Any(u => u.Name == "Trener"))
            {
                if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
                {
                    var store   = new UserStore <ApplicationUser>(context);
                    var manager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(store);
                    var user    = new ApplicationUser {
                        UserName = "******", Email = "*****@*****.**"
                    };

                    manager.Create(user, "Teodora!1");
                    context.Users.Add(user);
                    manager.AddToRole(user.Id, "Trener");
                }
            }

            /////////////////////
            ///

            if (!context.Roles.Any(r => r.Name == "Klijent"))
            {
                var store   = new RoleStore <IdentityRole>(context);
                var manager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(store);
                var role    = new IdentityRole {
                    Name = "Klijent"
                };

                manager.Create(role);
            }



            if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
            {
                var store   = new UserStore <ApplicationUser>(context);
                var manager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(store);
                var user    = new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**"
                };

                manager.Create(user, "Teodora!1");
                context.Users.Add(user);
                manager.AddToRole(user.Id, "Klijent");
            }
        }
Esempio n. 9
0
        protected override void Seed(DashoundCoachTravels.Models.ApplicationDbContext context)
        {
            var userManager = new Microsoft.AspNet.Identity.UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            //Creating new role types for accounts
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            IdentityResult roleResult;

            // Adding new role types named: Administrator, Employee, User
            if (!roleManager.RoleExists("Administrator")) { roleResult = roleManager.Create(new IdentityRole("Administrator")); }
            if (!roleManager.RoleExists("Employee")) { roleResult = roleManager.Create(new IdentityRole("Employee")); }
            if (!roleManager.RoleExists("User")) { roleResult = roleManager.Create(new IdentityRole("User")); }
            //Adding sample users: 1 for each role type testing only: Basic|Employee|Admin
            var sampleAccount = userManager.FindByEmail("*****@*****.**");
            if (sampleAccount == null)
            {
                var hasher = new PasswordHasher();
                sampleAccount = new ApplicationUser()
                {
                    UserName = "******",
                    Email = "*****@*****.**",
                    EmailConfirmed = true,
                    PasswordHash = hasher.HashPassword("user"),
                    Name = "Jon",
                    Surname = "Doe",
                    Town = "aaa",
                    Street = "streetA",
                    NumHouse = "1",
                    NumFlat = "12",
                    ZIPCode = "22-222",
                    Country = "Uganda"
                };
                userManager.Create(sampleAccount);
                userManager.AddToRole(sampleAccount.Id, "User");
                context.SaveChanges();
            }
            sampleAccount = userManager.FindByEmail("*****@*****.**");
            if (sampleAccount == null)
            {
                var hasher = new PasswordHasher();
                sampleAccount = new ApplicationUser()
                {
                    UserName = "******",
                    Email = "*****@*****.**",
                    EmailConfirmed = true,
                    PasswordHash = hasher.HashPassword("employee"),
                    Name = "Jonatan",
                    Surname = "Doppey",
                    Town = "bbb",
                    Street = "streetB",
                    NumHouse = "2",
                    NumFlat = "186",
                    ZIPCode = "66-666",
                    Country = "Nigeria"
                };
                userManager.Create(sampleAccount);
                userManager.AddToRole(sampleAccount.Id, "Employee");
                context.SaveChanges();
            }
            sampleAccount = userManager.FindByEmail("*****@*****.**");
            if (sampleAccount == null)
            {
                var hasher = new PasswordHasher();

                sampleAccount = new ApplicationUser()
                {
                    UserName = "******",
                    Email = "*****@*****.**",
                    EmailConfirmed = true,
                    PasswordHash = hasher.HashPassword("admin"),
                    Name = "Jonny",
                    Surname = "Dope",
                    Town = "ccc",
                    Street = "streetC",
                    NumHouse = "79",
                    NumFlat = "6",
                    ZIPCode = "99-971",
                    Country = "Egypt"
                };
                userManager.Create(sampleAccount);
                userManager.AddToRole(sampleAccount.Id, "Administrator");
                context.SaveChanges();
            }

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.
        }