public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); var dbContext = context.OwinContext.Get <ApplicationDbContext>(); if (dbContext.GetUserCount() <= 0) { UOWUserProfile dbcontext = new UOWUserProfile(); IdentityResult result = null; var userModel = new Models.ApplicationUser { Email = "*****@*****.**", UserName = "******" }; try { Random rand = new Random(); var password = Helper.GetRandomAlphanumericString(6) + "3#"; result = await userManager.CreateAsync(userModel, password); if (result.Succeeded) { string code = await userManager.GenerateEmailConfirmationTokenAsync(userModel.Id); System.Web.Mvc.UrlHelper urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes); string callbackUrl = urlHelper.Action( "ConfirmEmail", "Account", new { userId = userModel.Id, code = code }, HttpContext.Current.Request.Url.Scheme ); await userManager.SendEmailAsync(userModel.Id, "Confirm your account", "Your Password : "******" , and Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); var RoleManager = context.OwinContext.GetUserManager <ApplicationRoleManager>(); var role = "Admin"; if (!await RoleManager.RoleExistsAsync(role)) { var roleCreate = RoleManager.Create(new IdentityRole(Guid.NewGuid().ToString(), role)); if (!roleCreate.Succeeded) { throw new SystemException("User Tidak Berhasil Ditambah"); } } var addUserRole = await userManager.AddToRoleAsync(userModel.Id, role); if (!addUserRole.Succeeded) { throw new SystemException("User Tidak Berhasil Ditambah"); } var usesr = dbcontext.AddNewUser(new DataAccesLayer.Models.petugas { Email = "*****@*****.**", Name = "Administrator", Role = role, UserId = userModel.Id }); } throw new SystemException("User Tidak Berhasil Ditambah"); } catch (Exception ex) { context.SetError("App Not Yet Ready", "if you administrator please verify your account." + ex.Message); return; } } ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } if (!user.EmailConfirmed) { context.SetError("Email Confirm", "Your Account Not Yet Varification, Please Check Your Email"); return; } if (user.LockoutEnabled) { context.SetError("Lock", "Sory Your Account Is Lock, Please Contact Trireksa Administrator"); return; } ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager); ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager); AuthenticationProperties properties = CreateProperties(user.UserName, user.Roles); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); }
public async Task <IHttpActionResult> AddNewUser(petugas item) { UOWUserProfile context = new UOWUserProfile(); IdentityResult result = null; var userModel = new Models.ApplicationUser { Email = item.Email, UserName = item.Email }; try { if (item == null || !ValidateData(item)) { throw new SystemException("Lengkapi Data Anda"); } Random rand = new Random(); var password = Helper.GetRandomAlphanumericString(6) + "3#"; result = await UserManager.CreateAsync(userModel, password); if (result.Succeeded) { string code = await UserManager.GenerateEmailConfirmationTokenAsync(userModel.Id); System.Web.Mvc.UrlHelper urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes); string callbackUrl = urlHelper.Action( "ConfirmEmail", "Account", new { userId = userModel.Id, code = code }, HttpContext.Current.Request.Url.Scheme ); await UserManager.SendEmailAsync(userModel.Id, "Confirm your account", "Your Password : "******" , and Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); if (!await RoleManager.RoleExistsAsync(item.Role)) { var roleCreate = RoleManager.Create(new IdentityRole(Guid.NewGuid().ToString(), item.Role)); if (!roleCreate.Succeeded) { throw new SystemException("User Tidak Berhasil Ditambah"); } } var addUserRole = await UserManager.AddToRoleAsync(userModel.Id, item.Role); if (!addUserRole.Succeeded) { throw new SystemException("User Tidak Berhasil Ditambah"); } item.UserId = userModel.Id; var user = context.AddNewUser(item); if (user != null) { return(Ok(user)); } } throw new SystemException("User Tidak Berhasil Ditambah"); } catch (Exception ex) { if (result != null && result.Succeeded) { UserManager.Delete(userModel); } return(BadRequest(ex.Message)); } }