1

I'm trying to call a PartialView from within a page, the PartialView has IsAuthenticated, but the User is null, is there a way to populate this information (and be secure)?

My view is calling a Controller's ActionResult like this:

@Ajax.ActionLink(
    "Update Admin Menu",
    "/Admin/Menu/GetMenu?MenuType=AdminMenu",
    null,
    null,
    new AjaxOptions
    {
        HttpMethod = "GET",
        InsertionMode = InsertionMode.Replace,
        OnBegin = "",
        OnComplete = "",
        UpdateTargetId = "user-menu"
    },
    new { style = "btn btn-info" }
)

Then, the PartialView:

@if (User.Identity.IsAuthenticated)
{
    //return details only if logged in.
}

System.NullReferenceException: 'Object reference not set to an instance of an object.'

I don't believe this is possible, but I thought I would ask this community. Thank you!

Derek
  • 546
  • 6
  • 16
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jdphenix Apr 06 '18 at 15:48
  • 1
    @jdphenix: I don't think OP's question is really about the null-ref, I think it's more about how to access the identity. – Stefan Apr 06 '18 at 15:49
  • I would edit the top answer of the duplicate to include that information then. – jdphenix Apr 06 '18 at 16:04

1 Answers1

2

Any view that needs to be protected under authentication should have the [Authorize] Attribute in the calling controller. This will only allow authorized users to see any returned actionresults of method.

Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
Ms LDP
  • 43
  • 5
  • Makes sense, complicating this a bit further...what if I needed information about the User to render the Menu? Would be best to send the UserID in the Ajax request? – Derek Apr 06 '18 at 16:09