public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { await _initializer.CreateRolesAsync(); await _userManager.AddToRoleAsync(user, Input.Role.ToString()); _logger.LogInformation("User created a new account with password."); //Wstępnie nie chcemy potwierdzenia e-mail //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Page( // "/Account/ConfirmEmail", // pageHandler: null, // values: new { userId = user.Id, code = code }, // protocol: Request.Scheme); //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", // $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); //await _signInManager.SignInAsync(user, isPersistent: false); //return LocalRedirect(returnUrl); Message = "User created"; return(Page()); } foreach (var error in result.Errors) { _logger.LogError($"Create user : {user} failed | Error : {error.Description}"); ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseRequestLocalization(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "exam", template: "StartExam/{code?}", defaults: new { controller = "ExamApproaches", action = "Index" }); routes.MapRoute( name: "egzamin", template: "Egzamin/{code?}", defaults: new { controller = "ExamApproaches", action = "Index" }); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); var serviceProvider = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope().ServiceProvider; var userManager = serviceProvider.GetService <UserManager <IdentityUser> >(); var roleMenager = serviceProvider.GetService <RoleManager <IdentityRole> >(); var initializer = new UserInitializer(roleMenager, userManager, Configuration); initializer.CreateRolesAsync().Wait(); initializer.CreateDefaultUser().Wait(); }