コード例 #1
0
 // Allow Initialization with an instance of ApplicationUser:
 public EditUserViewModel(ApplicationUser user)
 {
     this.UserName = user.UserName;
     this.FirstName = user.FirstName;
     this.LastName = user.LastName;
     this.Email = user.Email;
     this.Role = user.Role;
     if(user.BvLocation != null)  //this should be unnecesary once the query child object thing gets fixed
     this.BvLocationId = user.BvLocation.BvLocationId;
     this.UserId = user.Id;
 }
コード例 #2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
コード例 #3
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {

                var user = new ApplicationUser() { 
                    UserName = model.UserName,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    AccessLevel = model.AccessLevel,
                    UserEmail = model.UserEmail,
                    PhoneNumber = model.PhoneNumber,
                    BvLocation = lRepo.GetBvLocations().FirstOrDefault(bvl => bvl.BvLocationId == model.BvLocationId)
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    TempData["message"] = string.Format("{0} has been registered. Welcome :)", user.UserName);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
コード例 #4
0
 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
コード例 #5
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
コード例 #6
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {

                var user = new ApplicationUser() { 
                    UserName = model.UserName,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    Role = model.Role
                    
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    TempData["message"] = string.Format("{0} has been registered.", user.UserName);


                    var Db = new ApplicationDbContext();
                    var user2 = Db.Users.First(u => u.UserName == user.UserName);
                    user2.BvLocation = Db.BvLocations.Find(model.BvLocationId);
                    Db.Entry(user2).State = System.Data.Entity.EntityState.Modified;
                    await Db.SaveChangesAsync();

                    var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

                    if (rm.RoleExists("Admin") && rm.RoleExists("User"))
                    {
                        var idManager = new IdentityManager();
                        if (user.Role == "Admin")
                        {
                            idManager.AddUserToRole(user.Id, "Admin");
                        }
                        if (user.Role == "User")
                        {
                            idManager.AddUserToRole(user.Id, "User");
                        }
                    }

                    return RedirectToAction("UserIndex", "CRUD");
                }
                else
                {
                    AddErrors(result);
                }
            }

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