Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Do not redirect to login page with Cookie Authentication #1541

@ihavenonickname

Description

@ihavenonickname

My ConfigureServices method have the following code:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
                {
                    options.LoginPath = "/Login";
                    options.AccessDeniedPath = "/Login";
                    options.LogoutPath = "/Login";
                    options.ExpireTimeSpan = TimeSpan.FromHours(10);
                });

And my controllers have

[Authorize]

On top of them, which redirects to /Login when user is not authenticated.

This behaviour is fine for my regular mvc controllers.

The problem is for my rest-like endpoints (their responses should be json-only). When user is not logged in it automatically sends the raw html of the login page but the client is expecting a json.

(yes, the same application has both kinds of controllers)

How can I configure my application such that some controllers respond with 401 instead of redirecting to /Login?

I am using net core 2.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions