Authorize attribute are good to do some access control base on Action but suppose that I have some UI element in the layout that should note be output unless the user is authorize.
I could possibly set some boolean in the ViewBag but that is not the good solution I guess.
Somewhere in the Layout.cshtml:
@if (ViewBag.IsAuthorized)
{
<li>@Html.ActionLink("Index", "Admin")</li>
}
Let me know if there is a better solution.
Thanks.
Use the
WebPageRenderingBase.Userobject; assuming you’ve earlier set the user into the HttpContext, or on the Controller (which you really should be). Since you’re using theAuthorizeattribute I’m thinking there must be aUser– so hopefully the following should work.This
Usercan be accessed in Razor very easily:Would output the current user’s name. So an
ifblock that runs only if the user is authenticated would be:Outputs
"Hello"if the user is authenticated.Thanks to James below for reminding me that you can check
IsAuthenticatedoff theUserobject – I couldn’t find it initially – but then remembered it’s on theIdentity!