0

净 mvc 3 应用程序。

我的登录表单有问题。

当我尝试使用我的管理员用户登录时,第一次登录没有问题,但如果我尝试使用任何其他问题登录,我必须通过第二个登录页面登录。

你的第一种形式是这样的:

http://HOST.com/Login/Login

除了我的数据库管理员用户之外的任何其他用途我必须尝试一次然后获取此网址

http://HOST.com/Login/Login?ReturnUrl=%2f

重定向并重试,然后它会正确登录。每次都会发生。

这是我的 web.config 中表单部分的表单代码

<authentication mode="Forms">
  <forms loginUrl="Login/Login" timeout="200000" slidingExpiration="true">
  </forms>
</authentication>

    <authorization>
      <deny users="?" />
    </authorization>

感谢您的任何建议

登录表单文件:

using System;
using System.Web.Mvc;
using System.Web.Security;
using SAMPLE.Models;
using SAMPLE.Helpers;
using System.Web;
using System.Linq;
namespace SAMPLE.Controllers
{
    public class LoginController : Controller
    {
        public ActionResult Login(string username, string password, string returnUrl)
        {
            if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
            {
                if (UserAccount.IsValid(username, password))
                    RedirectFromLoginPage(username, returnUrl);
                else
                    ViewData["LoginMessage"] = "Incorrect username or password.";
            }

            return View();
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login", "Login");
        }
        private void RedirectFromLoginPage(string username, string returnUrl)
        {
            FormsAuthentication.SetAuthCookie(username, false);

            var userType = 0;
            var user =  UserAccount.SingleOrDefault(x => x.Username == username.ToLower());
            if(user!=null)
             userType = user.UserType;             

            if (userType == 2)
            {
                Response.Redirect("/Usrmgmt");
            }
            else
            {
                var privileges = SAMPLE.Helpers.SAMPLEContext.Privileges;

                if ((privileges & PrivilegeConstants.Home) == PrivilegeConstants.Home)
                    Response.Redirect("/");
                else if ((privileges & PrivilegeConstants.Budgets) == PrivilegeConstants.Budgets)
                    Response.Redirect("/Budget");

                else if ((privileges & PrivilegeConstants.Estimates) == PrivilegeConstants.Estimates)
                    Response.Redirect("/Estimate");

                else if ((privileges & PrivilegeConstants.EstimageCatalogue) == PrivilegeConstants.EstimageCatalogue)
                    Response.Redirect("/Labour");

                else if ((privileges & PrivilegeConstants.CRM) == PrivilegeConstants.CRM)
                    Response.Redirect("/CRM");

                else if ((privileges & PrivilegeConstants.JobStatus) == PrivilegeConstants.JobStatus)
                    Response.Redirect("/JobStatus");  

                else if ((privileges & PrivilegeConstants.UserManage) == PrivilegeConstants.UserManage)
                    Response.Redirect("/Administration");

                else
                    Response.Redirect("/");
            }
            //}
        }
    }
}
4

1 回答 1

1

请将您的 web.config 更改为:

<authentication mode="Forms">
   <forms loginUrl="~/Login/Login" timeout="2880" protection="All"/>
</authentication>

删除下面的代码,然后重试:

<authorization>
  <deny users="?" />
</authorization>
于 2012-04-10T07:45:08.323 回答