Burst of Ice
Burst of Ice

Reputation: 386

Show parts of view for certain role and hide a nav button from not logged in users

I created an MVC project in .net for school and used this code to only show certain parts of my view to certain users with the specified role.

public ActionResult About()
    {
        if (User.IsInRole("Begeleider"))
        {
            var client = new WebClient();
            var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
            var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
            ViewBag.Message = leerlingen;
        }
        return View();
    }

This works when I log in with the user that has the role 'Begeleider' but when I click the button in the nav I get an error in my cshtml. Which is logical since I call the code here but can't reach it when I'm not logged in with the right role. But how do I fix it then?

@{
ViewBag.Title = "Evaluaties";
var leerlingen = List<ASPNetMVCExtendingIdentity2Roles.Domain.Leerling>)ViewBag.Message;
}
<h2>@ViewBag.Title.</h2>
<h4>Leerlingen</h4>
<table>
@foreach (var leerling in leerlingen)
{
    <tr>
        <td>@leerling.Naam</td>
        <td>@leerling.Email</td>
    </tr>
}
</table>
<h4>Evaluaties</h4>
@* Here shall be the same code as above but for a Leerling himself he'll only be able to see himself and his own Evaluation(Evaluatie), Haven't figuerd it out yet. *@

The nav is this and the last li is the one that shouldn't be visible for not logged in users.

<div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("Roles", "Index", "Roles")</li>
                <li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
            </ul>
            @Html.Partial("_LoginPartial")
        </div>

Upvotes: 0

Views: 1167

Answers (2)

Burst of Ice
Burst of Ice

Reputation: 386

I found an answer like this, so only when you are logged in you can see the listitem

<div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                @if (Request.IsAuthenticated)
                {
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("Roles", "Index", "Roles")</li>
                    <li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
                }
                else
                {
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("Roles", "Index", "Roles")</li>
                }

            </ul>
            @Html.Partial("_LoginPartial")
        </div>

Upvotes: 1

LazZiya
LazZiya

Reputation: 5719

use the authorization attribute for action method :

//you may use it without role name: [Authorize]
[Authorize(Roles = "Begeleider")]
public ActionResult About()
    {
            var client = new WebClient();
            var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
            var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
            ViewBag.Message = leerlingen;

        return View();
    }

if you want to hide the link for users not in role use:

if(User.IsInRole("Evaluaties")){
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
}

Upvotes: 0

Related Questions