public async Task <ActionResult> RegisterCompany(RegisterViewModelCompany model, HttpPostedFileBase image) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext())); if (!roleManager.RoleExists("Admin")) { var role = new IdentityRole(); role.Name = "Admin"; roleManager.Create(role); } var roleresult = UserManager.AddToRole(user.Id, "Admin"); ApplicationDbContext db = new ApplicationDbContext(); Company c = new Company(); c.CompanyName = model.CompanyName.ToUpper(); c.CompanyDescription = model.CompanyDescription.ToUpper(); // c.CompanyLogo = model.CompanyLogo; var path = Server != null?Server.MapPath("~") : ""; c.SaveLogo(image, path, "/ProfileImages/"); c.ApplicationUserId = user.Id; db.Companies.Add(c); db.SaveChanges(); // to display the name in the Hello user! a claim is created UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, c.CompanyName)); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Companies")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> RegisterCompany(RegisterViewModelCompany model) { if (ModelState.IsValid) { // Todo: Create a folder for each Company var folderPath = Server.MapPath("~/Data/Companies/" + model.Email); // Chech if Folder is Exists Byte[] logoData = null; //string _logoFileName; if (!Directory.Exists(folderPath)) { // Create Folder Directory.CreateDirectory(Server.MapPath("~/Data/Companies/" + model.Email)); string fullPathInServer = "~/Data/Companies/" + model.Email + "/"; // Get file info of the logo if (model.LogoFileName != null) { string logoFileName = Path.GetFileNameWithoutExtension(model.LogoFileName.FileName); string logoFileExtension = Path.GetExtension(model.LogoFileName.FileName); string _logoFileName = logoFileName + logoFileExtension; model.Logo = fullPathInServer + _logoFileName; _logoFileName = Path.Combine(Server.MapPath(fullPathInServer), _logoFileName); model.LogoFileName.SaveAs(_logoFileName); // Todo: convert the Company uploaded Photo as Byte Array before save to DB/ ApplicationDbContext => AspNetUserTable if (Request.Files.Count > 0) { HttpPostedFileBase fileBase = Request.Files["LogoFileName"]; using (var binary = new BinaryReader(fileBase.InputStream)) { logoData = binary.ReadBytes(fileBase.ContentLength); } } } } var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.Name, LastName = model.Name, UserPhoto = logoData }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await UserManager.AddToRoleAsync(user.Id, "company"); // Add new company to the database Company newCompany = new Company { CompanySecondID = user.Id, Name = model.Name, Logo = model.Logo, Address = model.Adresse, City = model.City, PhoneNumber = model.PhoneNumber, Description = model.Description, Email = model.Email, Password = model.Password }; try { db.Companies.Add(newCompany); db.SaveChanges(); } catch (Exception) { } // Pour plus d'informations sur l'activation de la confirmation de compte et de la réinitialisation de mot de passe, visitez https://go.microsoft.com/fwlink/?LinkID=320771 // Envoyer un message électronique avec ce lien // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirmez votre compte", "Confirmez votre compte en cliquant <a href=\"" + callbackUrl + "\">ici</a>"); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // Si nous sommes arrivés là, un échec s’est produit. Réafficher le formulaire return(View(model)); }