示例#1
0
 // Allow Initialization with an instance of ApplicationUser:
 public EditUserViewModel(ApplicationUser user)
 {
     this.UserName = user.UserName;
     //thissssssssssssxxzxxxcsddccccccccccccccccccccccccccccccccss.FirstName = user.FirstName;
     //this.LastName = user.LastName;,,,,,,,,,,,,,,,,,
     this.Email = user.Email;
 }
示例#2
0
       bool AddUserAndRoles()
       {
           bool success = false;
 
           var idManager = new IdentityManager();
           success = idManager.CreateRole("Admin");
           if (!success == true) return success;
 
           success = idManager.CreateRole("CanEdit");
           if (!success == true) return success;
 
           success = idManager.CreateRole("User");
           if (!success) return success;
 
 
           var newUser = new ApplicationUser()
           {
               UserName = "******",
               FirstName = "Onst",
               LastName = "Wang",
               Email = "*****@*****.**"
           };
 
           // Be careful here - you  will need to use a password which will 
           // be valid under the password rules for the application, 
           // or the process will abort:
           success = idManager.CreateUser(newUser, "Password8");
           if (!success) return success;
 
           success = idManager.AddUserToRole(newUser.Id, "Admin");
           if (!success) return success;
 
           success = idManager.AddUserToRole(newUser.Id, "CanEdit");
           if (!success) return success;
 
           success = idManager.AddUserToRole(newUser.Id, "User");
           if (!success) return success;
 
           return success;
       }
示例#3
0
        // Enable initialization with an instance of ApplicationUser:
        public SelectUserRolesViewModel(ApplicationUser user)
            : this()
        {
            this.UserName = user.UserName;
            this.FirstName = user.FirstName;
            this.LastName = user.LastName;

            var Db = new commenergyContext();

            // Add all available roles to the list of EditorViewModels:
            var allRoles = Db.Roles;
             foreach(var role in allRoles)
            {

                // An EditorViewModel will be used by Editor Template:
                var rvm = new SelectRoleEditorViewModel(role);
                this.Roles.Add(rvm);
            }

            // Set the Selected property to true for those roles for
            // which the current user is a member:
            foreach (var userRole in Db.Roles)
            {
                var checkUserRole =
                    Roles.Find(r => r.RoleId == userRole.Id);
                checkUserRole.Selected = true;
            }
        }
示例#4
0
 // Return a pre-poulated instance of AppliationUser:
 public ApplicationUser GetUser()
 {
     var user = new ApplicationUser()
     {
         UserName = this.UserName,
         FirstName = this.FirstName,
         LastName = this.LastName,
         Email = this.Email,
        Password = this.Password
     };
     return user;
 }
示例#5
0
 public bool CreateUser(ApplicationUser user, string password)
 {
     var idResult = _userManager.Create(user, password);
     return idResult.Succeeded;
 }
示例#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
          }; 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);
        }
示例#7
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);
 }
示例#8
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,
                    FirstName = model.FirstName,
                    LastName = model.LastName
                };
                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);
        }