How to make login path without using cookie in asp.net core 6 mvc ??

Ranjeet Singh 1 Reputation point
2022-12-03T12:57:12.897+00:00

I want to make Authentication in ASP.NET core 6 MVC, help me for give login path for unauthenticated user. How can I make login path but I don't want to use cookie in this.

this is possible or not to use addAuthentication without cookie in program.cs file ??

Developer technologies ASP.NET ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-03T16:56:13.137+00:00

    The cookie is used to store and pass the authentication information with a request. What do plan on using instead?

    0 comments No comments

  2. Anonymous
    2022-12-05T08:24:33.907+00:00

    Hi @Ranjeet Singh ,

    I want to make Authentication in ASP.NET core 6 MVC, help me for give login path for unauthenticated user. How can I make login path but I don't want to use cookie in this.

    this is possible or not to use addAuthentication without cookie in program.cs file ??

    From your description, I assume you are using JWT authentication or another provider to authenticate the user. Instead of using cookie or Identity Authentication (If you are using asp.net core Identity, by default it will redirect to the login page without setting it in the program.cs file, and it will also use cookie to store the user identity information).

    If that is the case, you can try to use the StatusCodePages middleware to check the response status code, if the response status is Unauthorized, redirect to the Login page:

    app.UseHttpsRedirection();  
    app.UseStaticFiles();  
    app.UseStatusCodePages(async context => {  
        var request = context.HttpContext.Request;  
        var response = context.HttpContext.Response;  
      
        // you may also check requests path to do this only for specific methods         
        // && request.Path.Value.StartsWith("/specificPath")  
        if (response.StatusCode == (int)HttpStatusCode.Unauthorized)  
        {  
            response.Redirect("/Account/Login");  //redirect to the Account Controller login page.  
        }  
    });  
      
    app.UseRouting();  
      
    app.UseAuthentication();  
    app.UseAuthorization();  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.