Question

Cannot perform runtime binding on a null reference error

I am getting this error in a partial view.

loginUserExists = Membership.ValidateUser(UserNameLogin, PasswordUserLogin);
if (loginUserExists==false)
{
    return;
}
FormsAuthentication.SetAuthCookie(UserNameLogin, true);

This is my partial view which produces the error.

@*This is where the error happens*@
**@if (!Page.User.Identity.IsAuthenticated)**
{
    <li><a href="@Url.Action("LoginForm", "Home")">Login</a></li>
    <li><a href="@Url.Action("RegisterForm", "Home")">Register</a></li>
}
else
{

    <li><a href="@Url.Action("LoginForm", "Home")">Logout</a></li>
    if (Model.FirstName != null)
    {
        <li><label id="labUserName">@Model.FirstName</label></li>
    }
}

The partial view is nested in the Layout page.

Insight perhaps as to why I get this and how to overcome it?

regards

Was it helpful?

Solution

Try Request.IsAuthenticated instead:

@if (!Request.IsAuthenticated)
{
    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top