コード例 #1
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid && User.IsInRole("Admin"))
            {
                try
                {
                    // Create a profile, password, and link the local login before signing in the user
                    User user = new User(model.UserName, model.DisplayName);
                    if (await Users.Create(user) &&
                        await Secrets.Create(new UserSecret(model.UserName, model.Password)) &&
                        await Logins.Add(new UserLogin(user.Id, IdentityConfig.LocalLoginProvider, model.UserName)))
                    {
                        //await SignIn(user.Id, isPersistent: false); // Registering isn't open to the public. This is for admins, who are already signed in
                        return RedirectToAction("Manage", new { Message = ManageMessageId.RegisterSuccess });
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty, "Failed to create login for: " + model.UserName);
                    }
                }
                catch (DbEntityValidationException e)
                {
                    ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
                }
            }

            // If we got this far, something failed, redisplay form
            return View("Manage", model);
        }
コード例 #2
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
                ClaimsIdentity id = await HttpContext.GetExternalIdentity();
                if (id == null)
                {
                    return View("ExternalLoginFailure");
                }
                try
                {
                    // Create a local user and sign in
                    var user = new User(model.UserName);
                    if (await Users.Create(user) &&
                        await Logins.Add(new UserLogin(user.Id, model.LoginProvider, id.FindFirstValue(ClaimTypes.NameIdentifier))))
                    {
                        await SignIn(user.Id, id.Claims, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        return View("ExternalLoginFailure");
                    }
                }
                catch (DbEntityValidationException e)
                {
                    ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
                }
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
コード例 #3
0
 private void UpdateUserLastSeen(User user)
 {
     // Set user last seen
     var db = new SpindleDbContext();
     user.LastSeen = DateTime.Now;
     db.Entry(user).State = EntityState.Modified;
     db.SaveChanges();
 }
コード例 #4
0
 public ActionResult ChangeDisplayName(User model)
 {
     var db = new SpindleDbContext();
     if (ModelState.IsValid)
     {
         var profile = db.Users.Find(model.Id);
         profile.DisplayName = model.DisplayName;
         db.Entry(profile).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         User.Identity.RefreshDbContext();
         return RedirectToAction("Manage", new { Message = ManageMessageId.ChangeDisplayNameSuccess });
     }
     // Something went wrong?
     return View("Manage", model);
 }