Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor View IsAuthenticated not working as expected

I have created a simple MVC application that is using the .Net Membership Provider that is supplied with the new project.

I am trying to get the tabs to show correctly. I might not understand this right but here's my code:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>Suburban Customer Portal</h1>
            </div>
            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>
            <div id="menucontainer">
              <ul id="menu">

                @if (Request.IsAuthenticated)
                {
                  <li>@Html.ActionLink("ChangePassword", "ChangePassword", "Account")</li>
                }else { 
                  <li>@Html.ActionLink("Logon", "Logon", "Account")</li>
                  <li>@Html.ActionLink("Register", "Register", "Account")</li>
                }

                  <li>@Html.ActionLink("ContactUs", "ContactUs", "Home")</li>
              </ul>
            </div>
        </div>
        <div id="main">
            @RenderBody()
        </div>
        <div id="footer">
        </div>
    </div>
</body>
</html>

At this line:

@if (Request.IsAuthenticated)

I am trying to show the right tabs pending on if they are already authenticated. This is always coming out as true...

How should I be doing this? I apparently am not doing it the right way...

Thanks again!

like image 674
ErocM Avatar asked Sep 06 '12 16:09

ErocM


2 Answers

I think you should use

@if(User.Identity.IsAuthenticated)

like image 161
Jakub Konecki Avatar answered Oct 16 '22 07:10

Jakub Konecki


Well, your question is not very clear, but Request.IsAuthenticated in this line:

@if(Request.IsAuthenticated)

checks if the request has data about an authenticated user. If true, you link will be shown.

In your specific case the Change Password link will only be shown if the user is logged in.

It looks correct.

For the tabs to be hidden, that is, for Request.IsAuthenticated = false you must first logout so that the cookies that store login information are cleared from the user browser.

like image 2
Leniel Maccaferri Avatar answered Oct 16 '22 07:10

Leniel Maccaferri