public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (MyUserManager userManager = new MyUserManager()) { IdentityUser user = new IdentityUser(); if (FindUser(context.UserName, context.Password) != null) { user = userManager.FindByName(context.UserName); } //IdentityUser user2 = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); } }
/// <summary> /// Add user to role /// </summary> /// <param name="userName"></param> /// <param name="roleName"></param> public void removeUserFromRole(string userName, string roleName) { var user = new ApplicationUser() { UserName = userName }; var selectedUser = MyUserManager.FindByName(user.UserName); MyUserManager.RemoveFromRole(selectedUser.Id, roleName); }
protected override void Seed(Infrastructure.DbContext.MyIdentityDbContext context) { MyUserManager userManager = new MyUserManager(new UserStore <MyUser>(context)); MyRoleManager roleManager = new MyRoleManager(new RoleStore <MyRole>(context)); string roleName = "Administrator"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleManager.RoleExists(roleName)) { roleManager.Create(new MyRole(roleName)); } MyUser user = userManager.FindByName(userName); if (user == null) { userManager.Create(new MyUser { UserName = userName, Email = email }, password); user = userManager.FindByName(userName); } if (!userManager.IsInRole(user.Id, roleName)) { userManager.AddToRole(user.Id, roleName); } foreach (MyUser dbUser in userManager.Users) { if (dbUser.Country == Countries.None) { dbUser.SetCountryFromCity(dbUser.City); } } context.SaveChanges(); }
/// <summary> /// Add user to role /// </summary> /// <param name="userName"></param> /// <param name="roleName"></param> public void addUserToRole(string userName, string roleName) { var user = new ApplicationUser() { UserName = userName }; var selectedUser = MyUserManager.FindByName(user.UserName); //HttpContext.Current.Session["Error"] = user.UserName; //HttpContext.Current.Response.Redirect("/UserPages/ErrorPage.aspx"); MyUserManager.AddToRole(selectedUser.Id, roleName); }
public ExpertiseModel Get(int id, int expertiseId) { ExpertiseModel expertise = db.t_Expertise.Where(w => w.ExpertiseId == expertiseId).Select(s => new ExpertiseModel() { ExpertiseId = s.ExpertiseId, Staff = s.Staff, Reason = s.Reason, Conclusion = s.Conclusion, NumberExp = s.NumberExp, DateExp = s.DateExp, RequestId = s.RequestId, Staff2 = s.Staff2, IsServiceableEquipment = s.IsServiceableEquipment, IsWarrantyRepair = s.IsWarrantyRepair, IsOrganizationRepair = s.IsOrganizationRepair, IsPartsSupply = s.IsPartsSupply, IsServiceable = s.IsServiceable, IsForWriteoff = s.IsForWriteoff }).FirstOrDefault(); if (expertise == null) { expertise = new ExpertiseModel(); //expertise.ExpertiseId = id; StringBuilder nameOfMalfunctions = new StringBuilder(); t_Equipment equipment = db.t_Equipment.Find(id); IEnumerable <string> malfunctions = equipment.t_Malfunction.Select(s => s.Name).ToList(); //nameOfMalfunctions.Append("Выявлены неисправности следующих деталей:\n"); foreach (string Name in malfunctions) { nameOfMalfunctions.Append(Name + "\n"); } expertise.Reason = nameOfMalfunctions.ToString(); DecisionMessage decisionMessage = new DecisionMessage(id); expertise.Conclusion = decisionMessage.Text; expertise.NumberExp = db.t_Message.Select(s => s.LastNumber + 1).FirstOrDefault(); expertise.DateExp = DateTimeOffset.Now; // insert Position and FIO of staff var user = _userManager.FindByName(_userName); if (user != null) { expertise.Staff = user.Position + " " + user.FIO; } } /*else * { * expertise.NumberExp = * db.t_Message.Select(s => s.LastNumber).FirstOrDefault(); * expertise.DateExp = DateTime.Today; * }*/ if (expertise != null && String.IsNullOrWhiteSpace(expertise.Conclusion)) { DecisionMessage decisionMessage = new DecisionMessage(id); expertise.Conclusion = decisionMessage.Text; } if (expertise != null && String.IsNullOrWhiteSpace(expertise.Reason)) { StringBuilder nameOfMalfunctions = new StringBuilder(); t_Equipment equipment = db.t_Equipment.Find(id); IEnumerable <string> malfunctions = equipment.t_Malfunction.Select(s => s.Name).ToList(); //nameOfMalfunctions.Append("Выявлены неисправности следующих деталей:\n"); foreach (string Name in malfunctions) { nameOfMalfunctions.Append(Name + "\n"); } expertise.Reason = nameOfMalfunctions.ToString(); } // Изменить ИД пользователя на другого в t_UserExpertise if (expertise != null && String.IsNullOrWhiteSpace(expertise.Staff)) { /* var user = db.t_User.SingleOrDefault(w => w.UserName == _userName); * if (user != null) * { * expertise.Staff = user.Position + " " + user.FIO; * * var expertiseUser = db.t_Expertise.Find(expertiseId).t_User.FirstOrDefault(); * * if (expertiseUser != null) * { * db.t_Expertise.Find(expertiseId).t_User.Remove(expertiseUser); * db.SaveChanges(); * db.t_Expertise.Find(expertiseId).t_User.Add(user); * db.SaveChanges(); * } * }*/ var user = _userManager.FindByName(_userName); ExpertiseUser expertiseUser = new ExpertiseUser(); if (expertiseUser.ReplaceUserForExpertise(expertiseId, user.UserID)) { expertise.Staff = user.Position + " " + user.FIO; } } return(expertise); }
private Task<IEnumerable<Claim>> validationCallback(string userName, string password) { using (DbContext dbContext = MyCtx.Create()) using (MyUserStore userStore = new MyUserStore(dbContext)) using (MyUserManager userManager = new MyUserManager(userStore)) { var user = userManager.FindByName(userName); if (user == null) { return null; } if (!userManager.CheckPassword(user, password)) { return null; } ClaimsIdentity claimsIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); return Task.FromResult(claimsIdentity.Claims); } }