Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:06:09+00:00 2026-06-06T22:06:09+00:00

Authorize attribute are good to do some access control base on Action but suppose

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:06:10+00:00Added an answer on June 6, 2026 at 10:06 pm

    Use the WebPageRenderingBase.User object; assuming you’ve earlier set the user into the HttpContext, or on the Controller (which you really should be). Since you’re using the Authorize attribute I’m thinking there must be a User – so hopefully the following should work.

    This User can be accessed in Razor very easily:

    @User.Identity.Name
    

    Would output the current user’s name. So an if block that runs only if the user is authenticated would be:

    @if(User.Identity.IsAuthenticated)
    {
       Hello
    }
    

    Outputs "Hello" if the user is authenticated.

    Thanks to James below for reminding me that you can check IsAuthenticated off the User object – I couldn’t find it initially – but then remembered it’s on the Identity!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have statically allowed access to controllers/action methods using the standard Authorize attribute with
I made a new action filter (attribute, similar to [Authorize]) which authorizes access to
I have an secure application with an Authorize attribute on each action. [Authorize(Roles =
I have area and in that area I have Home/Index that contains Authorize attribute.
I have an asp.net mvc 3 site that with my own authorize attribute. When
I've read that to use the attribute [Authorize] on MVC, you just have to
Let's assume that we have a controller with some action which could look like:
I have [Authorize] attribute on the HomeController , whenever I am trying to access
I have a custom authorize attribute on my controllers and it is not being
I have an MVC site, secured using [Authorize] attributes, but have an issue on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.