Home > Software engineering >  Specific Username or ID to hide hyperlink ASP.Net
Specific Username or ID to hide hyperlink ASP.Net

Time:05-10

I currently have Sessions set up to show certain hyperlinks if a user is signed in or not. Is there a way to do this for specific Usernames or ID's?

Currently my session code looks like this-

@if (Session["UsernameSS"] != null)
{
        <td>
            @Html.ActionLink("Add Thing", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })

        </td>
}
else
            {
        <td>
           
            @Html.ActionLink("Details", "Details", new { id = item.ID })
           

        </td>
}

Is there a way to code it so that @if (Session["UsernameSS"] == "John Doe" then only he could see a certain hyperlink?

Any help is appreciated, thanks!

CodePudding user response:

Yes, you can convert your Session variable to a string (in this case) and then do the comparison:

@if (Session["UsernameSS"] != null)
{
    if(Convert.ToString(Session["UsernameSS"]) == "John Doe")
    {
        <!-- Show these links to John Doe ONLY -->
        <td>
            @Html.ActionLink("Add Thing", "Edit", new { id = item.ID }) |
        </td>   
    }
    else
    {
       <!-- Show these links to everyone else -->
        <td>
            @Html.ActionLink("Add Thing", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>       
    }
}
else
{
    <td>  
        @Html.ActionLink("Details", "Details", new { id = item.ID })
    </td>
}
  • Related