简体   繁体   English

MVC 5登录挂起,需要重新启动VS 2015

[英]MVC 5 Login hanging, requires restart of VS 2015

So I have a basic MVC5 solution, I've implemented Identity 2 to authorise the logins. 因此,我有一个基本的MVC5解决方案,我已经实现了Identity 2来授权登录。 I developed it on my home computer (Windows 10) with no issues at all, although I'm going to run it through some rigorous tests tonight, however I've just pulled the solution from TFS onto my work PC (Windows 7) and have found an issue with the log in process. 我在家用计算机(Windows 10)上开发了它,没有任何问题,尽管我今晚将通过一些严格的测试来运行它,但是我只是将解决方案从TFS移植到了工作PC(Windows 7)和在登录过程中发现问题。

Basically, the log in works fine, as does the logging out, but then at some point it will just hang, loading forever. 基本上,登录和注销都可以正常工作,但是在某些时候它会挂起并永久加载。 The only way to fix is to close the browser window, stop the debug and restart VS 2015. 修复的唯一方法是关闭浏览器窗口,停止调试,然后重新启动VS 2015。

Here is my login controller: 这是我的登录控制器:

        // GET: Login
    [AllowAnonymous]
    public ActionResult Index(string returnUrl)
    {
        return View();
    }

    // POST: Login
    [HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
    public ActionResult Index(LoginViewModel lvm, string returnUrl)
    {
        if (!ModelState.IsValid) return View(lvm);

        var up =  new UserProvider();
        var identity = up.FindAccount(lvm);
        if (identity == null) return View(lvm);

        Authentication.SignIn(new AuthenticationProperties
        {
            IsPersistent = lvm.RememberMe
        }, identity);

        if (IsValidReturnUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }

        return RedirectToAction("Index", "Home");
    } 
        // GET/POST: Logout
    public ActionResult Logout()
    {
        Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return RedirectToAction("Index", "Home");

    }

    public bool IsValidReturnUrl(string returnUrl)
    {
        return !string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl);
    }

The method FindAccount() uses the information provided in the LoginViewModel to access the database. 方法FindAccount()使用LoginViewModel中提供的信息来访问数据库。 Now, there are two tables for users, "Sellers" and "Customers". 现在,有两个用户表,“卖方”和“客户”。 These are separate entities with separate tables, and although at this point their database structure is identical, eventually they won't be. 这些是具有单独表的单独实体,尽管此时它们的数据库结构是相同的,但最终它们不会相同。

public class UserProvider
{


    public ClaimsIdentity FindAccount(LoginViewModel login)
    {
        var _db = new AppDbContext();

        var user = login.Login;
        var password = login.Password;

        if (IsAccountNumber(user))
        {
            int accountNo;
            int.TryParse(user, out accountNo);
            var seller = _db.Sellers.SingleOrDefault(s => s.SellerNo.Equals(accountNo)
                                                   && s.Password.Equals(password));
            if (seller != null)
            {
                return BuildIdentity(seller.Email, seller.FirstName, seller.SellerNo.ToString(), "Seller");
            }
            var cust = _db.Customers.SingleOrDefault(c => c.CustomerNo.Equals(accountNo) && c.Password.Equals(password));
            if (cust != null)
            {
                return BuildIdentity(cust.Email, cust.FirstName, cust.CustomerNo.ToString(), "Customer");
            }
        }
        else
        {
            var seller = _db.Sellers.SingleOrDefault(s => s.Email.Equals(user)
                                                   && s.Password.Equals(password));
            if (seller != null)
            {
                return BuildIdentity(seller.Email, seller.FirstName, seller.SellerNo.ToString(), "Seller");
            }
            var cust = _db.Customers.SingleOrDefault(c => c.Email.Equals(user) && c.Password.Equals(password));
            if (cust != null)
            {
                return BuildIdentity(cust.Email, cust.FirstName, cust.CustomerNo.ToString(), "Customer");
            }
        }
        return null;
    }

    public bool IsAccountNumber(string login)
    {
        int accountNo;
        return int.TryParse(login, out accountNo);
    }

    public ClaimsIdentity BuildIdentity(string email, string firstName, string role, string accountNo)
    {
        var identity = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.Name, email),
                    new Claim("FirstName", firstName),
                    new Claim("AccountNumber", accountNo),
                    new Claim(ClaimTypes.Role, role) 
                },
                DefaultAuthenticationTypes.ApplicationCookie,
                ClaimTypes.Name, ClaimTypes.Role);
        return identity;
    }
}

The log in also allows the user to enter either their account number or email address to log in. As you can see in the code above, it first checks if the log in detail is an account number, if so then it checks for that matching account/password combo in the database. 登录还允许用户输入他们的帐号或电子邮件地址来登录。如您在上面的代码中看到的,它首先检查详细的登录名是否是一个帐号,如果是,则它检查该匹配项。数据库中的帐户/密码组合。 It then does the same but for email if no account number is provided. 然后,如果没有提供帐号,则执行相同的操作,但对于电子邮件。

Now this works fine, but anywhere between 4-10 login/logouts is when it starts hanging. 现在这可以正常工作,但是开始挂起时介于4到10个登录/注销之间。 We've also discovered (My colleague is experiencing the same issue on his work machine, also Windows 7, VS 2015) that if you input correct information, that appears to make the deadlock/hang occur sooner. 我们还发现(我的同事在他的工作机上(Windows 7和VS 2015上也遇到相同的问题)),如果您输入正确的信息,似乎会使死锁/挂起更快地发生。

I've used a breakpoint on the submit and traced the stack to find where it's occuring, it appears to be on the Linq query to the database, it'll hang within VS and then when it finally resolves, I press F10 to step over and it exists the breakpoint debugging mode immediately, continuing the application (Which is now in the hanging state). 我在提交中使用了一个断点,并跟踪了堆栈以查找它的发生位置,它似乎在对数据库的Linq查询中,它将挂在VS中,然后当它最终解析时,我按F10来跳过并且它立即存在断点调试模式,继续应用程序(现在处于挂起状态)。

I've tried a myriad of things, rebuilding the solution, rebuilding the database, changing the localhost port, tweaking the redirects. 我尝试了许多事情,重建解决方案,重建数据库,更改localhost端口,调整重定向。 The "cumulative" nature of the deadlock to me seems to indicate there's a memory leak that's gradually filling up the IIS server, but I can't for the life of me figure out where. 僵局的“累积性”性质似乎表明内存泄漏正在逐渐填满IIS服务器,但是我无法终生弄清楚错误的位置。

One thing to note, once I close the browser and tell it to stop debugging, I get a message regarding iisexpress and func-evals and that I will need to terminate them to detach. 需要注意的一件事是,一旦关闭浏览器并告诉它停止调试,我就会收到有关iisexpress和func-evals的消息,并且需要终止它们才能分离。

Thanks. 谢谢。

Resolved, it turns out that something had corrupted during my upload from my home computer to TFS, so the version on TFS was faulty. 解决后,事实证明,在我从家用计算机上传到TFS的过程中,某些内容已损坏,因此TFS上的版本有问题。 Never had that happen before! 从来没有发生过!

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

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