public async Task <IActionResult> AddUser(int id, [FromServices] IServiceProvider serviceProvider, addUserModel users) { var UserManager = serviceProvider.GetRequiredService <UserManager <IdentityUser> >(); //Assign Admin role to the main User here we have given our newly registered //login id for Admin management IdentityUser user = new IdentityUser(); user = await UserManager.FindByEmailAsync(users.Email); if (user == null) { ModelState.AddModelError(string.Empty, "Invalid user email."); return(View()); } UserWebsites userWebsites = new UserWebsites(); userWebsites.UserEmail = users.Email; userWebsites.WebsiteId = id; _context.Add(userWebsites); await UserManager.AddToRoleAsync(user, users.RoleName); await _context.SaveChangesAsync(); return(View()); }
public async Task <IActionResult> Create(Websites websites) { //([Bind("Id,CreatedBy,DomainUrl,WebsiteName")] Websites websites) if (ModelState.IsValid) { // var userData = _context.AspNetUsers.SingleOrDefault(x => x.UserName == User.Identity.Name); var websitesData = _context.Websites.Where(x => x.CreatedBy == User.Identity.Name).ToList(); for (int i = 0; i < websitesData.Count; i++) { if (websitesData[i].WebsiteName == websites.WebsiteName) { if (websitesData[i].IsDeleted == false) { ModelState.AddModelError(string.Empty, "This website name already exists . Please choose another name .. "); return(View()); } } } _context.Add(websites); _context.SaveChanges(); var websiteID = websites.Id; Widget widget = new Widget(); widget.CreatedDate = DateTime.Now; widget.CreatedBy = User.Identity.Name; widget.Title = websites.WebsiteName + "-Document"; widget.WidgetOrdinal = 0; try { int widgettypeid = _context.WidgetType.SingleOrDefault(x => x.Name == "Document").Id; widget.WidgetTypeId = widgettypeid; } catch { WidgetType widgetType = new WidgetType(); widgetType.Name = "Document"; widgetType.Description = "Base Page for any website"; widgetType.Section = "Body"; _context.Add(widgetType); await _context.SaveChangesAsync(); int widgettypeid = _context.WidgetType.SingleOrDefault(x => x.Name == "Document").Id; widget.WidgetTypeId = widgettypeid; } widget.HtmlBody = "<!DOCTYPE html>" + "< html lang = 'en' >" + "< head >" + "< meta charset = 'UTF-8' >" + "< meta name = 'viewport' content = 'width=device-width, initial-scale=1.0' >" + "< meta http - equiv = 'X-UA-Compatible' content = 'ie=edge' > " + "< title > ${websiteName} </ title >" + "</ head >" + "< body >" + "</ body >" + "</ html >"; _context.Add(widget); await _context.SaveChangesAsync(); UserWebsites userWebsites = new UserWebsites(); userWebsites.WebsiteId = websiteID; userWebsites.UserEmail = User.Identity.Name; _context.Add(userWebsites); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(websites)); }