简体   繁体   中英

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. 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.

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.

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. 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. 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.

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).

I've tried a myriad of things, rebuilding the solution, rebuilding the database, changing the localhost port, tweaking the redirects. 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.

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.

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. Never had that happen before!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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