简体   繁体   English

MVC 5 ASP.Net FORM Submit()函数不起作用

[英]MVC 5 ASP.Net FORM submit() function not working

I am trying to get a login form to work using the below method. 我正在尝试使用以下方法获取登录表单。 I am using JQuery version 1.12.0 and MVC5. 我正在使用JQuery版本1.12.0和MVC5。 i just create a accounts controller, when the admin login in, he/she can do CRUD functionality, others users can just see the posts and comments and nothings, but the login page not working means when I press Login nothing happens. 我只是创建一个帐户控制器,当管理员登录时,他/她可以执行CRUD功能,其他用户只能看到帖子和评论,但什么都没有,但是登录页面不起作用意味着当我按Login时没有任何反应。 The page does not refresh. 页面不会刷新。 It behaves as is I am clicking nothing this is login view 它的行为就像我没有点击任何东西一样, 这是登录视图

@model string

@{
  ViewBag.Title = "Login";
 }

 @section ExtraHeaders
  {
  <script src="@Url.Content("~/Scripts/Login.js")" type="text/javascript</script>
<script src="@Url.Content("~/Scripts/SHA256.js")" type="text/javascript</script>
 }

 <form action="@Href("~/Accounts/Login")" method="post" id="loginForm">
  <input type="text" name="name" id="name"/> Name <br />
  <input type="password" name="password" id="password"/> Password <br />
  <input type="hidden"  name="nonce" id="nonce" value="@Model"/>
  <input type="hidden"  name="hash" id="hash" value="hash"/>
  <input type="button" onclick="getPasswordHash('password', 'nonce','hash'); $('#loginForm').submit();" value="Login"/>
 </form>

this is login js file code 这是登录js文件的代码

function getPasswordHash (passwordElement, nonceElement, hashElement)
{
   var password = $('#' + passwordElement).attr('value');
  var nonce = $('#' + nonceElement).attr('value');
  $('#' + hashElement).attr('value', $.sha256(password + nonce));
  $('#' + passwordElement).attr('value', '');

 }

this is account controller for login 这是用于登录的帐户控制器

private BlogModel model= new BlogModel();

    public ActionResult Login(string name, string hash)
    {
        if(string.IsNullOrWhiteSpace(hash))

        {
            Random random = new Random();
            byte[] randomData = new byte[sizeof(long)];
            random.NextBytes(randomData);
            string newNonce = BitConverter.ToInt64(randomData, 0).ToString("X16");
            Session["Nonce"] = newNonce;
            return View(model: newNonce);

        }

        Administrator admin = model.Administrators.Where(x => x.Name == name).FirstOrDefault();
        string nonce = Session["Nonce"] as string;
        if(admin == null || string.IsNullOrWhiteSpace(nonce))
        {
            return RedirectToAction("Index", "Posts");

        }
        string computedHash;
        using (SHA256 sha256 = SHA256.Create()) //sha256
        {
            byte[] hashInput = Encoding.ASCII.GetBytes(admin.Password + nonce);
            byte[] hashData = sha256.ComputeHash(hashInput);
            StringBuilder stringBuidler= new StringBuilder();
            foreach(byte value in hashData)
            {
                stringBuidler.AppendFormat("{0:X2}", value);

            }
            computedHash = stringBuidler.ToString();

        }
        Session ["IsAdmin"]= (computedHash.ToLower() == hash.ToLower());
        return RedirectToAction("Index","Posts");
    }


    public ActionResult Logout()
    {
        Session["Nonce"] = null;
        Session["IsAdmin"] = null;
        return RedirectToAction("Index", "Posts");

    }
    public ActionResult Index()
    {
        return View();
    }

EDIT: getPasswordHash when i right click on its definition, it tell me "failed" either because the caret is already at the definition or because an explicit definition could not be found 编辑:getPasswordHash当我右键单击其定义时,它告诉我“失败”,因为插入符已在定义处或因为找不到显式定义

Decorate your action-method using HTTP post attribute 使用HTTP post属性装饰您的操作方法

 [HttpPost]
 public ActionResult Login(string name, string hash)
 {
 }

Http get and post more info Http获取并发布更多信息

The simplest approach would be to not use that JavaScript in the first place. 最简单的方法是首先不要使用该JavaScript。 It's not accomplishing anything, since you have server-side code to compute the hash anyway. 它没有完成任何事情,因为无论如何您都有服务器端代码来计算哈希。 And it's even potentially a security risk, since it exposes the hash computation (including any salt you use) to anyone who wants to see it. 甚至有潜在的安全风险,因为它会将哈希计算(包括您使用的所有盐)公开给任何想看的人。

Just remove it entirely and use a normal submit button: 只需将其完全删除并使用常规的提交按钮即可:

<input type="submit" value="Login"/>

Also, of course, remove this line: 另外,当然,请删除此行:

<input type="hidden"  name="hash" id="hash" value="hash"/>

And remove the hash parameter from the action method: 并从action方法中删除hash参数:

public ActionResult Login(string name)

There's probably more you can remove as well. 您可能还会删除更多内容。 For example, that "nonce" element and the corresponding model that's being passed to the view in the first place. 例如,首先将“ nonce”元素和相应的模型传递给视图。 (It really seems like there are a lot of moving parts involved in functionality that shouldn't be there in the first place.) (实际上,似乎功能中涉及了许多移动部件,这些部件最初不应该存在。)


Though what's not clear is why your action method isn't accepting the password as a parameter? 虽然不清楚为什么您的操作方法不接受密码作为参数? Normally a login form involves checking a password... 通常,登录表单涉及检查密码...

If it's the call to getPasswordHash that is causing an error, or something in that function, here's a few things to check: 如果是对getPasswordHash的调用导致错误或该函数中的某些内容,则需要检查以下几件事:

1) Are both javascript files Login.js and SHA256.js definitely loading? 1)javascript文件Login.jsSHA256.js是否SHA256.js加载?

2) If yes, if you view-source for the page in the browser, does <input type="hidden" name="nonce" id="nonce" value="@Model"/> have a value set? 2)是,如果您在浏览器中查看页面的源代码, <input type="hidden" name="nonce" id="nonce" value="@Model"/>是否设置了值? Your controller doesn't seem to set a value for the model getting passed into the view, and given SHA256 references the nonce value, this could be causing a problem. 您的控制器似乎没有为传递到视图中的模型设置值,并且给定SHA256引用了现时值,这可能会引起问题。

3) Given that Login.js references SHA256.js, and not the other way around, put the call to SHA256.js above Login.js 3)假设Login.js引用SHA256.js,而不是相反,将对SHA256.js的调用放在Login.js上方

Like David says, if this is a tutorial that you're learning from then OK but otherwise it's best doing the password hash at the server and not the client. 就像David所说的那样,如果这是您正在学习的教程,那么可以,但是否则最好在服务器而不是客户端上进行密码哈希。

First of all, use MVC3 Jquery 1.5 and SHA256 V1.0 From Alex Weber. 首先,使用Alex Weber的MVC3 Jquery 1.5和SHA256 V1.0。

Secondly, the code is defective. 其次,代码有缺陷。

I am also following the "From Zero to Blog" series. 我也关注“从零到博客”系列。 At this moment I am debugging the hash generating code because I have no problems logging in. 目前,我正在调试哈希生成代码,因为登录没有问题。

My problem is it doesn't give me the "delete" and "edit" option after logging in. 我的问题是登录后没有提供“删除”和“编辑”选项。

While debugging the code this is correct, the 2 hashes 在调试代码时这是正确的,这两个哈希

Session["IsAdmin"] = (computedHash.ToLower() == hash.ToLower()); 

from AccountController.cs which are compared in this line aren't the same, and have had completely different values every time. 在此行进行比较的AccountController.cs中的值不相同,并且每次都有完全不同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM