开发者

Have to type login credentials twice before entering system - MVC3

开发者 https://www.devze.com 2023-03-22 22:00 出处:网络
I am developing an ASP.NET MVC 3 application and I am using Forms Authentication to handle all the login functionality. I have created 3 user roles - Manager, Clients and Tutors and users will be dire

I am developing an ASP.NET MVC 3 application and I am using Forms Authentication to handle all the login functionality. I have created 3 user roles - Manager, Clients and Tutors and users will be directe开发者_开发百科d to different views depending on their role. My login was working fine before I coded the roles. Here is the code from my controller:

 public class AccountController : Controller
{

    //
    // GET: /Account/LogOn

    public ActionResult LogOn()
    {
        return View();
    }


    [Authorize(Roles="Manager")]
    public ActionResult Index()
    {
        return View();
    }

    [Authorize(Roles = "Tutors")]
    public ActionResult TutorMain()
    {
        return View();
    }

    [Authorize(Roles = "Clients")]
    public ActionResult ClientMain()
    {
        return View();
    }

    //
    // POST: /Account/LogOn

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    if (User.IsInRole("Manager"))
                    return RedirectToAction("Index", "Account");

                    if (User.IsInRole("Tutors"))
                    return RedirectToAction("TutorMain", "Account");

                    if (User.IsInRole("Clients"))
                    return RedirectToAction("ClientMain", "Account");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Please help me figure out why this is happening.

Thanks, Amy


Make sure that your login is a member of all the relevant roles .... ie if you are only a member of "Manager", then you would be prompted again if you try and view TutorMain() which requires "Tutors" Role.

You can specify multiple roles for each controller like so:

[Authorize(Roles="Manager")]
public ActionResult Index()
{
...
}

[Authorize(Roles = "Manager, Tutors")]
public ActionResult TutorMain()
{
        return View();
}

If this becomes bothersome, then investigate "custom" Authorize attributes - as described in asp.net mvc Adding to the AUTHORIZE attribute

0

精彩评论

暂无评论...
验证码 换一张
取 消