2

I have implemented cookie authentication together with Novel ldap authentication in my .net core application. I have a login screen where the users enters their credentials and once authenticated it redirects them to Home page. On my Login layout page I want to have a logout link which logouts the user out of the application. Earlier in my .net mvc application I could do:

 @if (Request.IsAuthenticated)
 {
   <li><a href="#">Hello, @ViewData["FullName"] !</a></li>
   <li><a href="@Url.Action("Logout", "Account")">Log Out</a></li>
 }
 else
 {
   <li><a href="@Url.Action("Login", "Account")">Log In</a></li>
 }

All my authentication, redirects etc work fine but whats the equivalent of Request.IsAuthenticated in .net core or how can I check if the user is authenticated and show links etc accordingly.

Would appreciate inputs

0

3 Answers 3

2

I think what you are looking for is

User.Identity.IsAuthenticated
Sign up to request clarification or add additional context in comments.

2 Comments

this does not work. Does not throw any errors but simply does not show the logout link.
This works after the user is authenticated, which makes it pretty useless. When your token cache has valid tokens, it incorrectly tells you that you're not authenticated.
2

What svek shared above should work. Debug to see if you are authenticating properly, you are setting up your cookies fine.

Specifically for .net core this should work.

 @if (User.Identity.IsAuthenticated)
 {
   <a asp-area="" asp-controller="YouController" asp-action="YourAction">Logout</a>
 }

Comments

0

The current MVC templates are using

@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

...

if (SignInManager.IsSignedIn(User))
{
 <li class="nav-item">
   <a class="nav-link text-dark" asp-area="" asp-controller="Teams" 
    asp-action="Index">Teams</a>
 </li>
 <li class="nav-item">
   <a class="nav-link text-dark" asp-area="" asp-controller="Match" 
    asp-action="Index">Spiele</a>
 </li>
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.