脚手架帐户页面无法访问

时间:2019-06-06 15:15:46

标签: c# asp.net .net asp.net-core

我正在ASP.NET Core应用程序中实现支架式身份,但是我无法访问大部分帐户页面。他们以前工作过,不知道为什么他们不再工作了。

我已经尝试过重新搭建页面,不幸的是没有成功。

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddSingleton<IConfiguration>(Configuration);

            var cultureInfo = new CultureInfo("nl-NL");
            cultureInfo.NumberFormat.NumberGroupSeparator = ",";

            services.Configure<RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture("nl-NL");
                //By default the below will be set to whatever the server culture is. 
                options.SupportedCultures = new List<CultureInfo> { new CultureInfo("nl-NL") };
            });

            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("nl-NL");
            CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("nl-NL");

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
            Configuration.GetConnectionString("defaultConnection")));

            //Vernieuwde Identityuser manier i.v.m. Role based Authorization
            services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

            //Login redirect juiste manier
            //services.ConfigureApplicationCookie(options => options.LoginPath = "~/Areas/Identity/Pages/Account/login");
            //services.ConfigureApplicationCookie(options => options.LogoutPath = "~/Areas/Identity/Pages/Account/logout");
            services.AddMvc();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddRazorPagesOptions(options =>
                {
                    options.AllowAreas = true;
                    options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
                });

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = $"/Identity/Account/Login";
                options.LogoutPath = $"/Identity/Account/Logout";
                options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
                options.Cookie.Name = "LoginSession";
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
            });

            services.Configure<IISOptions>(options =>
            {
                options.AutomaticAuthentication = false;
            });
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
        {

            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();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            CreateRoles(serviceProvider).Wait();
        }

在上述方法(Startup.cs)中,我可能会缺少一些所需的代码吗? 想到它以前没有其他原因。

1 个答案:

答案 0 :(得分:0)

注册新用户时,asp.net核心身份将调用IEmailSender中的Areas.Identity.Pages.Account.Register.cshtml.cs服务以确认电子邮件地址:

await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
   $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

如果您不想使用默认模板上的任何邮件发送功能,则可以删除每页上的IEmailSender相关代码。

否则,您可以按照this document来实现IEmailSender并将该IEmailSender注册到该文档显示的ASP.NET Core DI容器中。