Cookie 身份验证在 ASP.NET Core 应用程序中不起作用,我收到错误 http 405

问题描述 投票:0回答:1

Cookie 身份验证在 ASP.NET Core 应用程序中不起作用,我收到 http 405 错误。

请帮助我,我尝试了很多方法,但没有任何效果。

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using part23aspnetcorerProjeCore.Models;
using System.Security.Claims;

namespace part23aspnetcorerProjeCore.Controllers
{
    public class LoginController : Controller
    {
        Context c = new Context();

        [HttpGet]
        public IActionResult LoginPanel()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> GirisYap(Admin p)
        {
            if (!ModelState.IsValid)
            {
                var bilgiler = c.Admins.FirstOrDefault(x => x.Kullanici == p.Kullanici && x.Sifre == p.Sifre);

                if (bilgiler != null)
                {
                    var claims = new List<Claim>
                    {
                        new Claim(ClaimTypes.Name,p.Kullanici)
                    };

                    var useridentity = new ClaimsIdentity(claims, "Login");
                    ClaimsPrincipal principal = new ClaimsPrincipal(useridentity);
                    await HttpContext.SignInAsync(principal);

                    return RedirectToAction("/Personelim/Index/");
                }
            }
            //else
            //{
            //  return View("GirisYap");
            //}
            //else
            //{
            //  ModelState.AddModelError("", "Invalid UserName or Password");
            //  return View();
            //}
            return View();
        }
    }
}
@model part23aspnetcorerProjeCore.Models.Admin
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <title>Blend Login Form Flat Responsive Widget Template :: w3layouts</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="Blend  Login Form template Responsive, Login form web template,Flat Pricing tables,Flat Drop downs  Sign up Web Templates, Flat Web Templates, Login sign up Responsive web template, SmartPhone Compatible web template, free WebDesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
    <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
    <!-- Custom Theme files -->
    <link href="~/web/css/style.css" rel="stylesheet" type="text/css" media="all" />
    <link href="~/web/css/font-awesome.css" rel="stylesheet"> <!-- Font-Awesome-Icons-CSS -->
    <!-- //Custom Theme files -->
    <!-- web font -->
    <link href="//fonts.googleapis.com/css?family=Vollkorn+SC:400,600,700,900&amp;subset=cyrillic,cyrillic-ext,latin-ext,vietnamese" rel="stylesheet">
    <link href="//fonts.googleapis.com/css?family=Acme" rel="stylesheet">
    <!-- //web font -->
</head>
<body>
    <!-- main -->
    <div class="main">
        <h1>Asp.Net Core Mvc Learning Login Form</h1>
        <div class="main-w3lsrow">
            <!-- login form -->
            <div class="login-form login-form-left">
                <div class="agile-row">
                    <h2>Login Here</h2>
                    <div class="login-agileits-top">
                        <form action="#" method="post">
                            <p>User Name </p>
                            <input type="text" class="name" name="Kullanici" required="" />
                            <p>Password</p>
                            <input type="password" class="password" name="Sifre" required="" />
                            <label class="anim">
                                <input type="checkbox" class="checkbox">
                                <span> Remember me ?</span>
                            </label>
                            
                            <input type="submit" value="Login">
                            @*<form asp-controller="Login" asp-action="GirisYap" method="post">
                                <input type="submit" value="Giriş Yap" />
                            </form>*@
                            <
                            @*<form class="form-group" method="post" asp-controller="Login" asp-action="GirisYap">
                                <button class="btn btn-info">Giris Yap</button>
                            </form>*@
                        </form>
                    </div>
                    <div class="login-agileits-bottom">
                        <h6><a href="#">Forgot password?</a></h6>
                    </div>
                    <div class="login-agileits-bottom1">
                        <h3>(or)</h3>

                    </div>
                    <div class="social_icons agileinfo">
                        <ul class="top-links">
                            <li><a href="#"><i class="fa fa-facebook"></i></a></li>
                            <li><a href="#"><i class="fa fa-twitter"></i></a></li>
                            <li><a href="#"><i class="fa fa-linkedin"></i></a></li>
                            <li><a href="#"><i class="fa fa-google-plus"></i></a></li>
                        </ul>
                    </div>

                </div>
            </div>
        </div>
        <!-- copyright -->
        <div class="copyright">
            <p> © 2018 Blend  Login Form. All rights reserved | Design by <a href="http://w3layouts.com/" target="_blank">W3layouts</a></p>
        </div>
        <!-- //copyright -->
    </div>
    <!-- //main -->
</body>
</html>
c# asp.net-core authentication http controller
1个回答
0
投票

从您提供的代码来看,视图是通过

HttpGet LoginPanel
操作呈现的,因此 URL 是
Login/LoginPanel
。在表单中您使用
action="#" method="post"
属性,当您提交它时,表单将提交给 LoginController 中的
HttpPost LoginPanel
操作,但您在 LoginController 中只有
HttpGet LoginPanel
操作,因此会显示 405 错误。

所以在这里你只需要使表单将提交到正确的操作,在你的代码中,你似乎希望将表单提交到

GirisYap
操作,你只需要更改

action="#"

asp-action="GirisYap" or action="GirisYap"
© www.soinside.com 2019 - 2024. All rights reserved.