Esempio n. 1
0
        public void LocalLogin(string Email, string Password)
        {
            if (!String.IsNullOrEmpty(Email) && !String.IsNullOrEmpty(Password))
            {
                PickrWebService api    = new PickrWebService();
                JSONParser      parser = new JSONParser();
                UserDetails     user   = new UserDetails();

                JObject json = api.UserAuthentication(Email, Password);

                bool valid = parser.ParseUserAuthentication(json);

                // If the user exists then give acces

                if (valid)
                {
                    json = api.GetUser(Email);
                    user = parser.ParseUser(json);

                    //if (IsValid)
                    //{
                    ApplicationUserManager   manager;
                    ApplicationSignInManager signinManager;
                    SignInStatus             result;

                    try
                    {
                        // Validate the user password
                        manager       = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
                        signinManager = Context.GetOwinContext().GetUserManager <ApplicationSignInManager>();

                        // This doen't count login failures towards account lockout
                        // To enable password failures to trigger lockout, change to shouldLockout: true
                        result = signinManager.PasswordSignIn(Email, Password, RememberMe.Checked, shouldLockout: false);
                        if (result == SignInStatus.Failure)
                        {
                            //Register in the Sign in manager local database if user doesn't exist
                            var registerResult = manager.Create(new ApplicationUser()
                            {
                                UserName = Email, Email = Email
                            }, Password);
                            if (registerResult.Succeeded)
                            {
                                result = signinManager.PasswordSignIn(Email, Password, RememberMe.Checked, shouldLockout: false);
                            }
                        }

                        switch (result)
                        {
                        case SignInStatus.Success:

                            Response.Cookies["SoonNotification"].Value = "0";
                            HttpContext.Current.Session["User"]        = user;

                            //IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                            if (user.Mode.Equals("driver"))
                            {
                                Response.Redirect("/DriverHome");
                            }
                            else
                            {
                                Response.Redirect("/PassengerHome");
                            }
                            break;

                        case SignInStatus.LockedOut:
                            Response.Redirect("/Account/Lockout");
                            break;

                        case SignInStatus.RequiresVerification:
                            Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                                                            Request.QueryString["ReturnUrl"],
                                                            RememberMe.Checked),
                                              true);
                            break;

                        case SignInStatus.Failure:
                        default:
                            FailureText.Text     = "Invalid login attempt";
                            ErrorMessage.Visible = true;
                            break;
                        }
                    }
                    catch (DataException e)
                    {
                        FailureText.Text     = "Error while logging in, please try again";
                        ErrorMessage.Visible = true;
                    }


                    //}
                }

                else
                {
                    FailureText.Text     = "Invalid email or password";
                    ErrorMessage.Visible = true;
                }
            }
        }