开发者

IF statement not being executed C# MVC

开发者 https://www.devze.com 2023-01-31 23:29 出处:网络
I have an MVC application, which has two areas - Administrator, and User. What I wanted to do, is on first login, the User will activate their account, agree to terms and conditions, etc. This applica

I have an MVC application, which has two areas - Administrator, and User. What I wanted to do, is on first login, the User will activate their account, agree to terms and conditions, etc. This application uses the asp membership api.

I have inserted an IF statement into the POST login action in the Account controller, however after debugging, it appears that this if statement is never executed. If someone could take a look at it I'd be very grateful. I have a feeling it could be something small I'm missing.

The login URL for the User is:

http://localhost:80/Account/LogOn?ReturnUrl=/User

The administrator URL is:

http://localhost:80/Account/LogOn?ReturnUrl=/Administrator

however both use the same login action in the account controller so hence the need to use an if statement to differentiate between the two.

Here is the code for the POST Logon action. The string returnUrl is "/User" not "User" which I discovered after debugging through the code.

 [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        string url = returnUrl;

                        if (url == "/User")
                        {
                            ModelContainer ctn = new ModelContainer();

                            MyApp.Data.User p = ctn.Users.First(t => t.UserID == UserServices.CurrentUserId);

                            string status = p.AccountStatus;

                            if (status != "Active")
                            {
                                return RedirectToAction("contract", "Home");
                            }
                            else
                            {
                                return RedirectToAction("Index", "Home");
                            }
                        }
                        else
                            return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

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

It just seems to skip after the else here:

if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        string url = returnUrl;

and jumps right down to the final RedirectToAction.

I'd be very grateful if anyone could spot anything. If more info is needed just shout!


This will not go further than the if case when returnUrl == "/User" or any other value

if (!String.IsNullOrEmpty(returnUrl))
{
    return Redirect(returnUrl);
}

If not is null or empty, and if returnUrl == "/User", this is true since "/User" is not null and it is not empty.

and your second part will always be null or empty:

string url = returnUrl;

if (url == "/User")
{

so this will "appear" like something isn't working.

My suggestion would be to return the "true" case first in an if/ else for clarity:

if (string.IsNullOrEmpty(returnUrl))
{
    // do the default cases
}
else
{
    // redirect to returnUrl
}


It doesn't - from your code, returnUrl will always contain a string, so it will run the first part of your conditional statement, and just return the redirect - it will never hit your first time user code.

0

精彩评论

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