public async Task <IActionResult> Join(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new CodeCampUser { UserName = model.Email, Email = model.Email, Name = model.Name }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); await _mailService.SendTemplateMailAsync(model.Name, model.Email, "Confirm your account", "ConfirmEmail", callbackUrl); _logger.LogInformation(3, "User created a new account with password."); return(View("ResendConfirmEmailSent")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <TokenModel> GenerateForUser(CodeCampUser user, string optionsKey = "TokenOptions") { _config.Bind(optionsKey, _tokenOptions); // Create the token var claims = new List <Claim>() { new Claim(JwtRegisteredClaimNames.Sub, user.Email), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName) }; var userClaims = await _userManager.GetClaimsAsync(user); claims.AddRange(userClaims); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenOptions.SigningKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512); var token = new JwtSecurityToken( _tokenOptions.Issuer, _tokenOptions.Audience, claims, expires: DateTime.Now.AddMinutes(_tokenOptions.ExpirationLength), signingCredentials: creds); return(new TokenModel() { Token = new JwtSecurityTokenHandler().WriteToken(token), Expiration = token.ValidTo }); }
public async Task <IActionResult> Join(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new CodeCampUser { UserName = model.Email, Email = model.Email, Name = model.Name }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); if (!(await _mailService.SendTemplateMailAsync("ConfirmEmail", new AccountConfirmModel() { Name = model.Name, Email = model.Email, Subject = "Confirm your account", Callback = callbackUrl }))) { _logger.LogError($"Failed to send out confirmation email, user created but can't confirm the account."); ModelState.AddModelError("", "Could not send out confirmation email, please contact us at [email protected] for help."); } _logger.LogInformation(3, "User created a new account with password."); return(View("ResendConfirmEmailSent")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public Speaker MigrateSpeakerForCurrentUser(string moniker, CodeCampUser user) { var speaker = GetSpeakerForCurrentUser(moniker, user.UserName); if (speaker != null) { return(speaker); // Failsafe } // Test for user name otherwise use slug for older speakers var slug = user.Name.Replace(" ", "-"); var oldSpeaker = _ctx.Speakers .Where(s => s.UserName == user.UserName || s.Slug.ToLower() == slug.ToLower()) .OrderByDescending(s => s.Event.EventDate) .FirstOrDefault(); // Not an old speaker if (oldSpeaker == null) { return(null); } speaker = new Speaker(); _mapper.Map(oldSpeaker, speaker); var currentEvent = GetEventInfo(moniker); speaker.Event = currentEvent; speaker.UserName = user.UserName; AddOrUpdate(speaker); return(speaker); }
public async Task SeedAsync() { try { if (_env.IsDevelopment()) { if (_config["Data:IterateDatabase"].ToLower() == "true") { await _ctx.Database.EnsureDeletedAsync(); await _ctx.Database.EnsureCreatedAsync(); } var admin = await _userManager.FindByEmailAsync(_config["Admin:SuperUser:Email"]); // If no Admin, then we haven't seeded the database if (admin == null) { admin = new CodeCampUser() { UserName = _config["Admin:SuperUser:Email"], Email = _config["Admin:SuperUser:Email"], Name = _config["Admin:SuperUser:Name"], EmailConfirmed = true }; // Create Super User if (!(await _userManager.CreateAsync(admin, _config["Admin:SuperUser:TempPassword"])).Succeeded) { throw new InvalidOperationException("Failed to create Super User"); } if (!(await _roleManager.CreateAsync(new IdentityRole(Consts.ADMINROLE))).Succeeded) { throw new InvalidOperationException("Failed to create Admin Role"); } // Add to Admin Role if (!(await _userManager.AddToRoleAsync(admin, Consts.ADMINROLE)).Succeeded) { throw new InvalidOperationException("Failed to update Super User Role"); } } EventInfo[] codeCamps; if (!_ctx.CodeCampEvents.Any()) { codeCamps = new EventInfo[] { new EventInfo() { Moniker = "2017", Name = "Atlanta Code Camp 2017", EventDate = new DateTime(2017, 9, 17), EventLength = 1, Description = "The Atlanta Code Camp is awesome! The Atlanta Code Camp is awesome! The Atlanta Code Camp is awesome!", IsDefault = true, TwitterLink = "https://twitter.com/atlcodecamp", ContactEmail = "*****@*****.**", CallForSpeakersOpened = new DateTime(2017, 5, 25), CallForSpeakersClosed = new DateTime(2016, 9, 1), Location = new EventLocation() { Facility = "Kennesaw State University", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2016", Name = "Atlanta Code Camp 2016", EventDate = new DateTime(2016, 10, 15), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = true, TwitterLink = "https://twitter.com/atlcodecamp", ContactEmail = "*****@*****.**", CallForSpeakersOpened = new DateTime(2016, 7, 1), CallForSpeakersClosed = new DateTime(2016, 10, 1), Location = new EventLocation() { Facility = "Kennesaw State University", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2015", Name = "Atlanta Code Camp 2015", EventDate = new DateTime(2015, 10, 24), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = false, CallForSpeakersOpened = new DateTime(2015, 8, 1), CallForSpeakersClosed = new DateTime(2015, 10, 1), Location = new EventLocation() { Facility = "Kennesaw State University (Formerly Southern Polytechnic)", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2014", Name = "Atlanta Code Camp 2014", EventDate = new DateTime(2014, 10, 11), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = false, CallForSpeakersOpened = new DateTime(2014, 8, 1), CallForSpeakersClosed = new DateTime(2014, 9, 15), Location = new EventLocation() { Facility = "Southern Polytechnic", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2013", Name = "Atlanta Code Camp 2013", EventDate = new DateTime(2013, 8, 24), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = false, CallForSpeakersOpened = new DateTime(2013, 6, 1), CallForSpeakersClosed = new DateTime(2013, 8, 1), Location = new EventLocation() { Facility = "Southern Polytechnic", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } } }; _ctx.AddRange(codeCamps); await _ctx.SaveChangesAsync(); } else { codeCamps = _ctx.CodeCampEvents.ToArray(); } if (!_ctx.Sponsors.Any()) { var sponsor = new Sponsor() { Name = "Wilder Minds", Link = "http://wilderminds.com", Event = codeCamps[0], Paid = true, ImageUrl = "/img/2016/sponsors/wilder-minds.jpg", SponsorLevel = "Silver" }; _ctx.Add(sponsor); sponsor = new Sponsor() { Name = "Wilder Minds", Link = "http://wilderminds.com", Event = codeCamps[1], Paid = true, ImageUrl = "/img/2016/sponsors/wilder-minds.jpg", SponsorLevel = "Silver" }; _ctx.Add(sponsor); _ctx.AddRange(Add2015Sponsors(codeCamps.Where(s => s.Moniker == "2015").First())); _ctx.AddRange(Add2014Sponsors(codeCamps.Where(s => s.Moniker == "2014").First())); _ctx.AddRange(Add2013Sponsors(codeCamps.Where(s => s.Moniker == "2013").First())); await _ctx.SaveChangesAsync(); } if (!_ctx.Speakers.Any()) { await Migrate(); } } } catch (Exception ex) { // TODO ex.ToString(); } }