bool ValidLogin(Login login)
        {
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
                UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(userStore)
                {
                    UserLockoutEnabledByDefault = true,
                    DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0),
                    MaxFailedAccessAttemptsBeforeLockout = 5
                };
                var user = userManager.FindByName(login.UserName);

                if (user == null)
                {
                    UserNoFound = true;
                    return false;
                }

                // User is locked out.
                if (userManager.SupportsUserLockout && userManager.IsLockedOut(user.Id))
                {
                    Locked = true;
                    return false;
                }

                // Validated user was locked out but now can be reset.
                if (userManager.CheckPassword(user, login.Password) && userManager.IsEmailConfirmed(user.Id))

                {
                    if (userManager.SupportsUserLockout
                     && userManager.GetAccessFailedCount(user.Id) > 0)
                    {
                        userManager.ResetAccessFailedCount(user.Id);
                    }
                }
                // Login is invalid so increment failed attempts.
                else {
                    bool lockoutEnabled = userManager.GetLockoutEnabled(user.Id);
                    PasswordIncorrent = true;
                    if (userManager.SupportsUserLockout && userManager.GetLockoutEnabled(user.Id))
                    {
                        userManager.AccessFailed(user.Id);
                        return false;
                    }
                }
                return true;
        }
        public ActionResult Index(Login login)
        {
            // UserStore and UserManager manages data retreival.
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
            IdentityUser identityUser = manager.Find(login.UserName,
                                                             login.Password);

            if (ModelState.IsValid)
            {
                if (ValidLogin(login))
                {
                    IAuthenticationManager authenticationManager
                                           = HttpContext.GetOwinContext().Authentication;
                    authenticationManager
                   .SignOut(DefaultAuthenticationTypes.ExternalCookie);

                    var identity = new ClaimsIdentity(new[] {
                                            new Claim(ClaimTypes.Name, login.UserName),
                                        },
                                        DefaultAuthenticationTypes.ApplicationCookie,
                                        ClaimTypes.Name, ClaimTypes.Role);

                    // SignIn() accepts ClaimsIdentity and issues logged in cookie.
                    authenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = false
                    }, identity);

                    Session[login.UserName] = "true";

                  //  Client client = context.Clients.Find(login.UserName);
                    Session["userProfile"] = context.Clients.Find(login.UserName).profile;

                    return RedirectToAction("Square", "Home", new { UserName = login.UserName });
                }
                else
                {
                    if (UserNoFound || PasswordIncorrent)
                    {
                        ViewBag.Message = "The username and password you entered did not match our records. Please double-check and try again.";
                    }
                    else if (Locked)
                    {
                        ViewBag.Message = "You have been locked. Please login in 10 minutes.";
                    }

                }
            }

            return View();
        }