简体   繁体   English

将其发布到Web托管服务后无法登录我的mvc网站

[英]Can't login to my mvc site after publishing it to a Web hosting service

I have a asp.net MVC website that I've recently published on a web hosting service. 我有一个最近在网络托管服务上发布的asp.net MVC网站。 Almost everything works great, the information that I get from my msSQL db is shown on the pages etc etc. But there's one problem. 几乎所有东西都运行良好,我从msSQL数据库获得的信息显示在页面等上。但是有一个问题。 When I try to log in to my website, it won't work. 当我尝试登录我的网站时,它将无法正常工作。 Before I explain any further, you'll have to take a look at the following code which is how I authenticate a user login: 在我进一步解释之前,您必须先看一下以下代码,这是我如何验证用户登录名的方式:

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

[HttpPost, ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid)
    {

        if (Repository.AuthenticateLogin(model.Username, model.Password.GetHashCode().ToString()))
        {
            FormsAuthentication.SetAuthCookie(model.Username, false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            TempData["WrongLogin"] = "The username or password you've entered is wrong. Please try again!";
            return View(model);
        }
    }
    else
    {
        return View(model);
    }
}

And the method "AuthenticateLogin(string username, string password) that is used in the if-statement above looks like this: 上面的if语句中使用的方法“ AuthenticateLogin(字符串用户名,字符串密码)看起来像这样:

public static bool AuthenticateLogin(string username, string password)
    {
        using (var context = new SHultEntities())
        {
            return (from u in context.User
                    where u.Username == username && u.Password == password
                    select u).Any();
        }
    }

Now, as you can see, to authenticate a user, I am checking the entered username and password against any user in the database's user-table. 现在,您可以看到,要对用户进行身份验证,我正在对照数据库用户表中的任何用户检查输入的用户名和密码。 If there is a match, the method returns "true", or else it returns "false". 如果存在匹配项,则该方法返回“ true”,否则返回“ false”。 Yeah, you get it. 是的,你明白了。

Well, the problem is, when I try to log in on my site I get the TempData["WrongLogin"] text, which means my AuthenticateLogin method must've returned false, which means that there must not have been any matches. 好吧,问题是,当我尝试登录我的网站时,我得到了TempData [“ WrongLogin”]文本,这意味着我的AuthenticateLogin方法必须返回false,这意味着必须不存在任何匹配项。 But when I try this on my local project on my computer, with the same username and password, the AuthenticateLogin returns true. 但是,当我在计算机上的本地项目中使用相同的用户名和密码尝试此操作时,AuthenticateLogin返回true。

If the problem was the connection to the database, the headers and contents that I retrieve from my database would not appear on the site. 如果问题是与数据库的连接,那么我从数据库检索到的标头和内容将不会显示在站点上。 But it does. 但是确实如此。 Also, when I update something from my local project and then go on my website, the updated information appears. 另外,当我从本地项目中更新某些内容然后进入我的网站时,将显示更新的信息。 So, the only problem is that I can't log in. 因此,唯一的问题是我无法登录。

This is driving me crazy and I would really appreciate some help. 这让我发疯,我将非常感谢您的帮助。

EDIT: Could be worth mentioning that I have to log in to my website to edit any content/information. 编辑:值得一提的是,我必须登录我的网站来编辑任何内容/信息。 That's why I mentioned that I can log in and change from my local project, and then the changes appear on the website. 这就是为什么我提到我可以登录并从本地项目进行更改,然后更改显示在网站上的原因。

FOUND PROBLEM: I tried creating a user with a non-hashed password and deleted the .GetHashCode.ToString() from the AuthenticateLogin on the password. 出现的问题:我尝试创建一个使用非哈希密码的用户,并从AuthenticateLogin上的密码中删除了.GetHashCode.ToString()。 Then i re-published. 然后我重新出版。 It works. 有用。 The problem is the hashing. 问题是散列。

How can I solve this? 我该如何解决? I need hashed passwords in the db... 我需要在数据库中使用哈希密码...

How did you seed the username and password in your database? 您是如何在数据库中植入用户名和密码的? If the implementation of password.GetHashCode() is machine specific (ie relies on a machine specific salt) then this could be why it cannot match against any users. 如果password.GetHashCode()的实现是特定于计算机的(即依赖于特定于计算机的盐),则这可能就是为什么它无法与任何用户匹配的原因。 On the other hand if the users were created via the remote (hosted) environment, this should not be a problem. 另一方面,如果用户是通过远程(托管)环境创建的,则应该没有问题。

The problem was caused by my hashing. 问题是由我的哈希引起的。 Apprenatly, two strings that looks exactly the same can produce different values when hashed, with standard framework implementation of .GetHashCode() , on different machines (even though the machines are using the same version of the framework). 适当地,使用.GetHashCode()的标准框架实现,在不同的机器上(即使机器使用的是相同版本的框架),两个看起来完全相同的字符串在散列时也可以产生不同的值。

For more information, click here! 有关更多信息, 请单击此处!

I Solved my problem by using a customized Hashing-class. 我通过使用自定义的哈希类解决了我的问题。

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

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