Search code examples
authenticationasp.net-coreasp.net-core-mvc

Validate if User/Request is authenticated .net core


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


Solution

  • 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>
     }