0

Here is my controller. Its not working for logged in users preventing access to the login page. Please help me out. I have used the request.Is Authenicated and User.Identity Methods in my controller

[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (Request.IsAuthenticated)
    {
        return RedirectToAction("Main","Home");
    }
   else if (!this.ModelState.IsValid)
    {
        return this.View(model);
    }

   else if (Membership.ValidateUser(model.Username, model.Password))
    {
        FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
        if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
        {
            return this.Redirect(returnUrl);
        }

        return this.RedirectToAction("Main", "Home");
    }
    this.ModelState.AddModelError(string.Empty,"Invalid Credentials.");
    this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");

    return this.View(model);
}
Samson Yerraguntla
  • 101
  • 1
  • 1
  • 7

2 Answers2

1

I tried this line in my code

bool test = Request.IsAuthenticated;

and it always came up false. However I am using windows Authentication in my application. If you have a logon screen I'm assuming you are not. I found this post on the site about always receiving a false value from Request.IsAuthenticated. Obviously your code should work IF you are receiving the right value from this flag. If this is not the answer let me know I might research a little more.

Request.IsAuthenticated is always false

Community
  • 1
  • 1
0

this is the way i do:

public bool IsAuthenticated
{
    get
    {
        return this._context.User != null && 
               this._context.User.Identity != null &&
               this._context.User.Identity.IsAuthenticated;
    }
}
Abiezer
  • 772
  • 8
  • 14