示例#1
0
 public static ClaimsIdentity CreateIdentity(UserModel user, string authenticationType)
 {
     var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
     identity.AddClaim(new Claim(ClaimTypes.Name, user.FirstName));
     identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
     identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity"));
     identity.AddClaim(new Claim("Id", user.Id.ToString()));
     return identity;
 }
示例#2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            var userModel = new UserModel { Email = model.Email};
            if (!ModelState.IsValid) return View(model);
            var user = _accountService.GetAccounts().FirstOrDefault(n => n.IsDeleted == false && n.Valid && n.Email == model.Email && n.Password == model.Password);
            if (user != null)
            {
                
                userModel.FirstName = user.FirstName;
                userModel.LastName = user.LastName;
                userModel.Id = user.Id;
                var identity = UserService.CreateIdentity(userModel, DefaultAuthenticationTypes.ApplicationCookie);
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe }, identity);
                return RedirectToAction("Index","Home");
            }
            ModelState.AddModelError("", "Invalid username or password.");

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }