1

I need to hide a link if a user is not logged in, and show the link if the user is logged in. I should use HTML. But the following:

 @if(Authorize(Roles = "admin")) <li>@Html.ActionLink(@Resources.LayoutLang.myarticles, "MyReviews", "Review")</li>

does not work. How to check the role in HTML?

6
  • 2
    @(if (User.IsInRole("admin")) { <li>....</li> } Commented Aug 23, 2015 at 11:26
  • Take a look at this answer it should help: stackoverflow.com/questions/6981853/… Commented Aug 23, 2015 at 11:45
  • Error - Role Manager feature is not included. But this.UserManager.AddToRoleAsync(user.Id, "Reviewer"); in controller is work Commented Aug 23, 2015 at 11:55
  • What is Authorizer? Is this some kind of authorization service? Commented Aug 23, 2015 at 12:16
  • I don't remember where find this))) Commented Aug 24, 2015 at 14:55

2 Answers 2

1

If you just want to check that the user is signed in:

@if (Request.IsAuthenticated)
{
    // do stuff
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use IsInRole

  @if (User.IsInRole("admin"))
    {
         <li>@Html.ActionLink(@Resources.LayoutLang.myarticles, "MyReviews", "Review")</li>
    }
    else
    {
        <li>Not logged in</li>
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.