public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } IdentityUser user = new IdentityUser { UserName = model.UserName, }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) { return(errorResult); } IdentityUser registerUser = UserManager.FindByName(user.UserName); wvc_user wvcUser = new wvc_user { aspnetuser_id = registerUser.Id, created_by = 1, created_date = DateTime.Now, is_active = true }; wvcUser.Save(); return(Ok()); }
private void CreateMember(string email, string password, string role, string firstName) { IdentityManager identity = new IdentityManager(); var adminUser = identity.GetUser(email); if (adminUser == null) { string errorResult = string.Empty; if (identity.CreateUser(email, password, out errorResult)) { adminUser = identity.GetUser(email); if (adminUser != null) { identity.AddUserToRole(adminUser.Id, role); using (WVCContext context = new WVCContext()) { var wvcUser = context.wvc_user.FirstOrDefault(q => q.aspnetuser_id == adminUser.Id); if (wvcUser == null) { wvcUser = new wvc_user(); wvcUser.aspnetuser_id = adminUser.Id; wvcUser.created_date = DateTime.Now; wvcUser.first_name = firstName; wvcUser.is_active = true; wvcUser.Save(); } } } } } }
private void CreateMember(string email,string password,string role,string firstName) { IdentityManager identity = new IdentityManager(); var adminUser = identity.GetUser(email); if(adminUser == null) { string errorResult = string.Empty; if(identity.CreateUser(email,password,out errorResult)) { adminUser = identity.GetUser(email); if(adminUser != null) { identity.AddUserToRole(adminUser.Id,role); using(WVCContext context = new WVCContext()) { var wvcUser = context.wvc_user.FirstOrDefault(q => q.aspnetuser_id == adminUser.Id); if(wvcUser == null) { wvcUser = new wvc_user(); wvcUser.aspnetuser_id = adminUser.Id; wvcUser.created_date = DateTime.Now; wvcUser.first_name = firstName; wvcUser.is_active = true; wvcUser.Save(); } } } } } }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (UserManager <IdentityUser> userManager = _userManagerFactory()) { IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); WVCUserManager wvcUserManager = new WVCUserManager(); IdentityManager identityManager = new IdentityManager(); wvc_user wvcUser = null; IdentityUserRole userRole = null; IdentityRole role = null; if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } else { userRole = user.Roles.FirstOrDefault(); if (userRole == null) { context.SetError("invalid_grant", "The user is inactive (no rules assigned). Contact administrator."); return; } role = identityManager.GetRoleById(userRole.RoleId); // check wvc user active; wvcUser = wvcUserManager.FindUser(user.Id); if (wvcUser == null) { context.SetError("invalid_grant", "The user is inactive. Contact administrator."); return; } } // Add claims ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); oAuthIdentity.AddClaim(new Claim(Authentication.IDKey, wvcUser.id.ToString())); oAuthIdentity.AddClaim(new Claim(Authentication.RoleKey, role.Name)); ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user, role, wvcUser); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); } }
public static AuthenticationProperties CreateProperties(IdentityUser user, IdentityRole role, wvc_user wvcUser) { IDictionary<string, string> data = new Dictionary<string, string> { { "user_name", user.UserName }, { "role", role.Name}, { "id", wvcUser.id.ToString()}, { "first_name", (wvcUser.first_name != null ? wvcUser.first_name : "")}, { "last_name", (wvcUser.last_name != null ? wvcUser.last_name : "")} }; return new AuthenticationProperties(data); }
public static AuthenticationProperties CreateProperties(IdentityUser user, IdentityRole role, wvc_user wvcUser) { IDictionary <string, string> data = new Dictionary <string, string> { { "user_name", user.UserName }, { "role", role.Name }, { "id", wvcUser.id.ToString() }, { "first_name", (wvcUser.first_name != null ? wvcUser.first_name : "") }, { "last_name", (wvcUser.last_name != null ? wvcUser.last_name : "") } }; return(new AuthenticationProperties(data)); }
public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if(!ModelState.IsValid) { return BadRequest(ModelState); } IdentityUser user = new IdentityUser { UserName = model.UserName, }; IdentityResult result = await UserManager.CreateAsync(user,model.Password); IHttpActionResult errorResult = GetErrorResult(result); if(errorResult != null) { return errorResult; } IdentityUser registerUser = UserManager.FindByName(user.UserName); wvc_user wvcUser = new wvc_user { aspnetuser_id = registerUser.Id, created_by = 1, created_date = DateTime.Now, is_active = true }; wvcUser.Save(); return Ok(); }