public HttpResponseMessage Login(LoginViewModel model) { HttpResponseMessage response = null; if (ModelState.IsValid) { BooksContext db = new BooksContext(); BookUserManager userManager = new BookUserManager(); var existingUser = db.Users.FirstOrDefault(u => u.UserName == model.UserName); if (existingUser == null) { response = Request.CreateResponse(HttpStatusCode.NotFound); } else { if (userManager.CheckPassword(existingUser, model.Password)) { object dbUser; var token = CreateToken(existingUser, out dbUser); response = Request.CreateResponse(new { dbUser, token }); } else { response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } } else { response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } return(response); }
private void createAdminUser() { BooksContext context = new BooksContext(); var roleManager = new BookRoleManager(); var userManager = new BookUserManager(); if (!roleManager.RoleExists("Admin")) { var role = new IdentityRole(); role.Name = "Admin"; roleManager.Create(role); var user = new IdentityUser(); user.UserName = "******"; user.Email = "*****@*****.**"; string userpwd = "admin123"; var chkUser = userManager.Create(user, userpwd); if (chkUser.Succeeded) { var result = userManager.AddToRole(user.Id, "Admin"); } } }