HttpContext.Current.User.IsInRole raises HttpUnhandledException. Why?

1.2k views Asked by At

While I was working on my localhost it worked flawlessly.

However, when I deployed the application to another server it started to raise an exception.

Basically, this is the instruction that causes the Exception:

HttpContext.Current.User.IsInRole(*roleName*)

where roleName is a string representing the different roles in the application.

Exception fired:

System.Web.HttpUnhandledException

Update: This is how the user is being authenticated.

protected void LoginControl_Authenticate(object sender, AuthenticateEventArgs e)
{
    Page.Validate();
    if (!Page.IsValid) return;

    if (MyAuthenticate(LoginControl.UserName, LoginControl.Password))
    {
        if (LoginControl.RememberMeSet == true)
        {
            createCookie(LoginControl.UserName, Convert.ToInt32(ViewState["idcustomer"]), true);
        }
        else
        {
            createCookie(LoginControl.UserName, Convert.ToInt32(ViewState["idcustomer"]), false);
        }
        e.Authenticated = true;
        Response.Redirect(FormsAuthentication.GetRedirectUrl(LoginControl.UserName, true));
    }
    else
    {
        e.Authenticated = false;
    }

If it is related to the user authentication, what am I missing? Thanks in advance.

UPDATE: Thanks to all. I have just done a Remote Debugging and realize this is the actual error:

enter image description here

Now, this is the situation: the User was authenticated in the login page. So, I think it is not a problem with the connection to the DB. Otherwise, the User wouldn't have been able to pass the login page to the MasterPage (where this error is being raised).

What can I be missing? Thanks again,

2

There are 2 answers

0
aleafonso On BEST ANSWER

The solution for this problem was to create the mysql_aspnet_membership provider by granting full trust through the machine.config and autogenerating the schema, instead of manually creating the membership tables in the database, which I though there could have been a valid way to create the db.

Afterwards, this didn't break anymore.

6
Coding Flow On

Wild stab in the dark but the inner exception is probably a null reference exception being caused by the fact the user is not authenticated on the server so HttpContext.Current.User is null. I would bet it is just that windows integrated authentication is not enabled on the live web server or a similar authentication setup issue.