コード例 #1
0
ファイル: Global.asax.cs プロジェクト: emiliobs/Democracy
        //Método para quemar el super usuario del sistema:
        private void CheckSuperUser()
        {
            //me conecto all db:
            var userContext = new ApplicationDbContext();
            //Aqui controlo los usuarios:
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
            var db = new DemocracyContext();
            this.CheckRole("Admin", userContext);
            this.CheckRole("User", userContext);

            //validar si el usuario existe en la tabla user:
            var user = db.Users.Where(u=>u.userName.ToLower().Equals("*****@*****.**")).FirstOrDefault();

            if (user == null)
            {
                //creamos nuestro usuario Administrador:
                user = new User
                {
                    Address = "Calle Madrid 55",
                    FirstName = "Emilio",
                    LastName = "Barrera",
                    Phone = "693661995",
                    userName = "******",
                    Photo = "~/Content/Photos/fondo.jpg"
                };

                db.Users.Add(user);
                db.SaveChanges();
            }

            //valido si el uusarip existe en las tablas de the ASP NET User:
            var userASP = userManager.FindByName(user.userName);

            if (userASP == null)
            {
                userASP = new ApplicationUser
                {
                  UserName = user.userName,
                  Email = user.userName,
                  PhoneNumber = user.Phone                
                 
                };

                userManager.Create(userASP, "Eabs+++++55555");
                userManager.AddToRole(userASP.Id, "Admin");
            }

            userManager.AddToRole(userASP.Id, "Admin");
        }
コード例 #2
0
        public async Task<ActionResult> Register(RegisterUserView userView)
        {
            if (ModelState.IsValid)
            {

                //Upload Image:
                string path = string.Empty;
                string picture = string.Empty;

                if (userView.Photo != null)
                {
                    picture = Path.GetFileName(userView.Photo.FileName);
                    path = Path.Combine(Server.MapPath("~/Content/Photos"), picture);
                    userView.Photo.SaveAs(path);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        userView.Photo.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();
                    }
                }

                //Save record:
                var user = new User
                {
                    Address = userView.Address,
                    FirstName = userView.FirstName,
                    Grade = userView.Grade,
                    Group = userView.Group,
                    LastName = userView.LastName,
                    Phone = userView.Phone,
                    Photo =  string.IsNullOrEmpty(picture) ? string.Empty : string.Format("~/Content/Photos/{0}", picture),
                    userName = userView.userName
                };
                var db = new DemocracyContext();
                db.Users.Add(user);

                try
                {
                    db.SaveChanges();
                    var userASP =  this.createASPUser(userView);

                    await SignInManager.SignInAsync(userASP, isPersistent: false, rememberBrowser: false);
                    return RedirectToAction("Index", "Home");
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null &&
                        ex.InnerException.InnerException != null
                        && ex.InnerException.InnerException.Message.Contains("userNameIndex"))
                    {
                        ModelState.AddModelError(string.Empty,"The Email has already used for another User.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }

                    return View(userView);
                }

              }

            // If we got this far, something failed, redisplay form
            return View(userView);
        }