登录页面时出现Asp .Net Core Razor 500 Http错误

时间:2019-03-20 16:54:47

标签: c# razor-pages asp.net-core-2.2

我从剃须刀页面开始,我创建了一个登录页面,并使用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();
    }
}

0 个答案:

没有答案