0

I am trying to save information about the login in the HttpContext.Current.User.Identity in a asp.net-mvc5 proyect. But allways is empty.

Firt of all, a have a View with controller. When you click on login button, call to the controller:

public ActionResult Button1_Click(string user, string pass)
{
    bool result = _model.ValidateLogin(user, pass, 3, false);

    DirectResult r = new DirectResult();

    // Do some Authentication...
    if (!result)
    {
        r.Success = false;
        r.ErrorMessage = "Invalid username or password.";
    }

    return r;
}

The method ValidateLogin, first check the user is on database, and with the id create a cookie:

DateTime now = DateTime.Now;
System.Web.Security.FormsAuthentication.Initialize();
System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1, userId.ToString(), now, now.Add(System.Web.Security.FormsAuthentication.Timeout), checkRemember, string.Empty, System.Web.Security.FormsAuthentication.FormsCookiePath);
string hash = System.Web.Security.FormsAuthentication.Encrypt(ticket);
System.Web.HttpCookie cookie = new System.Web.HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
 cookie.Expires = now.AddYears(1);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

But when i try to check the User.Identify always is empty and "IsAuthenticated" is false:

protected override System.Security.Principal.IIdentity GetClientIdentity()
{
    IIdentity identity = System.Web.HttpContext.Current.User.Identity;
    if (identity.IsAuthenticated)
        return identity;
    else
        throw new AuthorizationDeniedException("Not logged in",false);

}

Why? Any idea?

Edit for add web.config:

In the web config, i have:

 <authentication mode="Forms">
      <forms loginUrl="~/Login/Index" protection="All" timeout="120" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="default.html" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"/>
 </authentication>
 <authorization>
      <deny users="?"/>
 </authorization>
 <location path="Login">
      <system.web>
           <authorization>
                <allow users="*"/>
           </authorization>
      </system.web>
 </location>

1 Answer 1

1

I am assuming that you have missed to override FormsAuthentication_OnAuthenticate method in global.asax Checkout this sample for the solution http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF

Sign up to request clarification or add additional context in comments.

1 Comment

Just to point out Application_PostAuthenticateRequest is also listed if working with versions >=4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.