MVC登录无法正常工作

时间:2013-09-12 06:22:45

标签: asp.net-mvc-3 authentication login

我有一个MVC3应用程序,当我从VS运行解决方案时,我的登录模块工作正常。但是当我在IIS中添加它作为站点时(版本6)登录不起作用。我写了一个txt文件,看看登录是否失败,但登录成功。但它不允许我去主页,它指示我回到登录页面本身。请帮忙。

    [HttpPost]
            public ActionResult Login(LoginModel model, string returnUrl) {

                string lines = "UserName:" + model.Username + " Pwd:" + model.Password + " IsValid:" + ModelState.IsValid + "\n";
                System.IO.File.WriteAllText(@"E:\Nirushan.txt", lines);

                if (!ModelState.IsValid) {
                    ModelState.AddModelError("", GetLocalResource("Message.InvalidLogin"));
                    return View();
                }

                var authenticationResult = _authenticationService.TryAuthenticateDirectLogin(model.Username, model.Password);

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\Nirushan.txt", true))
                {
                    file.WriteLine();
                    //Status is success
                    file.WriteLine("Status:" + authenticationResult.AuthenticationStatus);
                    bool st = AuthenticationTicketService.SetAuthenticationTicket(authenticationResult.AuthenticatedUser.Id);
                    // st is true
                    file.WriteLine("TicketService:" + st);
                }

                if (authenticationResult.AuthenticationStatus == AuthenticationStatus.RegeneratedForgottenPasswordExpired) {
                    ModelState.AddModelError("", GetLocalResource("Message.RegeneratedForgottenPasswordExpired"));
                    return View();
                }

                if (authenticationResult.AuthenticationStatus == AuthenticationStatus.UserIsLockedOut) {
                    ModelState.AddModelError("", GetLocalResource("Message.UserIsLockedOut"));
                    return View();
                }

                if (authenticationResult.AuthenticationStatus == AuthenticationStatus.DirectLoginNotAllowed) {
                    ModelState.AddModelError("", GetLocalResource("Message.DirectLoginNotAllowed"));
                    return View();
                }

                if (authenticationResult.AuthenticationStatus != AuthenticationStatus.Success) {
                    ModelState.AddModelError("", GetLocalResource("Message.LoginFailed"));
                    return View();
                }

                if (authenticationResult.AuthenticatedUser.PasswordChange)
                    return RedirectToAction("ChangePassword", new {
                        returnUrl,
                        model.Username
                    });

                if (AuthenticationTicketService.SetAuthenticationTicket(authenticationResult.AuthenticatedUser.Id) == false) {
                    ModelState.AddModelError("", GetLocalResource("Message.MissingSessionInfo"));
                    return View();
                }

                return Redirect(returnUrl ?? Url.Action("Index", "Home"));
            }

0 个答案:

没有答案