【问题标题】:Scaffolded account pages not accessible脚手架帐户页面无法访问
【发布时间】:2019-06-06 15:15:46
【问题描述】:

我正在我的 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) 中遗漏了一些需要的代码? 想不出任何其他原因,因为它以前有效。

【问题讨论】:

  • 什么是导致身份管理不工作的错误信息?
  • @NanYu 在尝试激活“MyProjectName.Areas.Identity.Pages.Account.RegisterModel”时无法解析类型“Microsoft.AspNetCore.Identity.UI.Services.IEmailSender”的服务。
  • 在您的 DI 系统中添加 IEmailSender 见回复。

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


【解决方案1】:

注册新用户时,asp.net core Identity 会调用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 容器,如该文档所示。

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 2014-05-11
    • 1970-01-01
    • 2020-08-03
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多