public IEnumerable <string> CreateStudent(AppUser user, IAuthenticationManager manager, AppIdentityDbContext db) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(db)); UserRoleManager roleMgr = new UserRoleManager(new RoleStore <UserRoles>(db)); IdentityResult res = userMgr.Create(user, user.Password); userMgr.CheckPassword(user, user.Password); if (res.Succeeded && roleMgr.RoleExists("student")) { IdentityResult role = userMgr.AddToRole(user.Id, "student"); db.Students.Add(new Student { StudentName = user.UserName, }); ClaimsIdentity identity = userMgr.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); manager.SignOut(); manager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); return(null); } else { return(res.Errors); } }
public ClaimsIdentity GenerateUserIdentity(AppUserManager manager) { var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return(userIdentity); }
public Task <ClaimsIdentity> GenerateUserIdentity(AppUserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = manager.CreateIdentity <AppUser, int>(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return(Task.FromResult(userIdentity)); }
public ClaimsIdentity GenerateUserIdentity(AppUserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here var id = userIdentity.GetUserId <int>(); var user = manager.FindById(id); userIdentity.AddClaim(new Claim("User.FullName", user.FullName)); return(userIdentity); }
public static string GenerateToken(AppUserManager userManager, AppUser user) { ClaimsIdentity identity = userManager.CreateIdentity(user, Startup.OAuthBearerOptions.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties()); var currentUtc = new SystemClock().UtcNow; ticket.Properties.IssuedUtc = currentUtc; ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(700)); return(Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket)); }
public string LoginUser(AppUser guest, IAuthenticationManager manager, AppIdentityDbContext db) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(db)); AppUser user = userMgr.Find(guest.UserName, guest.Password); if (user == null) { return("Некорректное имя или пароль"); } else { ClaimsIdentity identity = userMgr.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); manager.SignOut(); manager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); return(string.Empty); } }
/// <summary> /// Method login logs in user after inserting user name and password /// </summary> /// <param name="userName">User's user name</param> /// <param name="password">User's password</param> /// <returns>Logged in user</returns> public ClaimsIdentity Login(string userName, string password) { var userManager = new AppUserManager(new AppUserStore(new AppDbContext())); var wantedUser = userManager.FindByName(userName); if (wantedUser == null) { return(null); } AppUser user = userManager.Find(wantedUser.UserName, password); if (user == null) { return(null); } return(userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie)); }
public IEnumerable <string> CreateUser(AppUser user, string password, IAuthenticationManager manager) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(db)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <UserRoles>(db)); IdentityResult res = userMgr.Create(user, password); userMgr.CheckPassword(user, password); if (password.Contains("12345")) { List <string> errors = res.Errors.ToList(); errors.Add("Пароль не должен содержать последовательность чисел"); return(errors); } else if (res.Succeeded && roleMgr.RoleExists("user")) { IdentityResult role = userMgr.AddToRole(user.Id, "user"); db.Accounts.Add(new Account { Id = user.Id, UserName = user.UserName, Email = user.Email, PhoneNumber = user.PhoneNumber }); ClaimsIdentity identity = userMgr.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); manager.SignOut(); manager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); return(null); } else { return(res.Errors); } }
public ClaimsIdentity Login(string email, string password) { var userManager = new AppUserManager(new AppUserStore(new AppDbContext())); try { var wantedUser = userManager.FindByEmail(email); if (wantedUser == null) { return(null); } var user = userManager.Find(wantedUser.UserName, password); return(user == null ? null : userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie)); } catch { return(null); } }
public IHttpActionResult Login(LoginModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = AppUserManager.FindByName(model.Username); if (user == null) { return(BadRequest(string.Format("User {0} does not exist.", model.Username))); } if (!AppUserManager.CheckPassword(user, model.Password)) { return(BadRequest("Invalid password.")); } var identity = AppUserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); var auth = Request.GetOwinContext().Authentication; auth.SignIn(identity); return(Ok()); }