首页 > 解决方案 > 转到登录页面时,Asp .Net Core Razor 500 Http 错误

问题描述

我从剃须刀页面开始,我创建了一个登录页面并使用 ldap 进行身份验证并登录。仍然没有注销选项。事情是这样的,我希望登录成为默认页面,所以我从剃刀模板中删除了每个默认页面,但错误只留下登录

Pages 
  Account Folder 
  Login page

问题是登录后,现在每当我尝试访问登录页面时,它都会给我这个错误

HTTP 错误 500.0 - ANCM 进程内处理程序加载失败 此问题的常见原因:未找到 Microsoft.NetCore.App 或 Microsoft.AspNetCore.App 的指定版本。进程中的请求处理程序 Microsoft.AspNetCore.Server.IIS 未在应用程序中引用。ANCM 找不到 dotnet。故障排除步骤:检查系统事件日志中的错误消息 启用记录应用程序进程的标准输出消息 将调试器附加到应用程序进程并检查有关详细信息,请访问: https ://go.microsoft.com/fwlink/?LinkID=2028526

我找不到有关此错误的任何信息,仅发生在一页上,仅与 IIS 有关。

这只发生在登录页面上,我可以正常访问其他页面...

这是我登录页面上的代码

@page
@model GestaoRequisicoes.Pages.LoginModel
@{
    ViewData["Title"] = "Login";
}

<h1>Login</h1>

<p>Autentique-se para iniciar sessão:</p>
<form method="post">

    <div class="container">
        <div class="row">
            <div class="col-md-12">

                <div class="form-group">
                    <div asp-validation-summary="All" class="text-danger"></div>
                    <label for="exampleInputPassword1">Username</label>
                    <input asp-for="loginData.Username" value="username" class="form-control form-control-sm" />
                </div>

                <div class="form-group">
                    <label for="exampleInputPassword1">Password</label>
                    <input asp-for="loginData.Password" value="password" class="form-control form-control-sm" />
                </div>

                <div class="form-group form-check">
                    <input id="txtRememberMe" asp-for="loginData.RememberMe" type="checkbox" class="form-check-input" />
                    <label class="form-check-label" for="txtRememberMe">Remember me</label>
                </div>

                <input type="submit" value="Login" class="btn btn-primary" />

            </div>
        </div>
    </div>

    @Html.AntiForgeryToken()
</form>

和cs文件

public class LoginModel : PageModel
{
    [BindProperty]
    public LoginData loginData { get; set; }

    public void OnGet()
    {            

    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (ModelState.IsValid)
        {                             
            string dominio = "10.35.14.240/OU=Users,OU=PLG,OU=PT,OU=EMEA,DC=sd6,DC=glb,DC=corp,DC=local";
            string adPath = "LDAP://" + dominio;

            LDAPAutenticador aut = new LDAPAutenticador(adPath);
            var isValid = aut.autenticado(dominio, loginData.Username, loginData.Password);

            if (!isValid)
            {
                ModelState.AddModelError("", "username or password is invalid");
                return Page();
            }
            else
            {
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
                identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));
                var principal = new ClaimsPrincipal(identity);
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = loginData.RememberMe });
                return RedirectToPage("Account/Index");
            }                
        }
        else
        {
            ModelState.AddModelError("", "username or password is blank");
            return Page();
        }
    }

    public class LoginData
    {
        [Required]
        [StringLength(8)]
        public string Username { get; set; }

        [Required, DataType(DataType.Password)]
        public string Password { get; set; }

        public bool RememberMe { get; set; }
    }
}

和启动文件

 public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        }).AddCookie(options => { options.LoginPath = "/Login"; });

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


        services.AddMvc().AddRazorPagesOptions(options => 
        {
            options.Conventions.AuthorizeFolder("/Account");
            options.Conventions.AllowAnonymousToPage("/Login");

        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc();
    }
}

更新

已经有一段时间了,但如果有人注意到这样的错误,只需检查 Windows 事件查看器,它会告诉您错误消息。该消息与实体框架的连接池超时异常有关。我假设 Entity Framework 在我刷新 SQL Server 资源管理器上的数据库之前无法连接到 SQL Server,可能是因为我正在使用 localdb\mssqlserver 实例进行测试,但这仍然是一种奇怪的行为,并且只会在每天第一次运行项目时发生

标签: c#razor-pagesasp.net-core-2.2

解决方案


推荐阅读