Redirect to URL instead of 401 for unauthenticated

删除回忆录丶 提交于 2019-12-04 15:25:23

There's no such property for a simple reason: the JWT bearer middleware (like the more generic OAuth2 middleware in Katana) has been designed for API authentication, not for interactive authentication. Trying to trigger a redirection in this case wouldn't make much sense for headless HTTP clients.

That said, it doesn't mean that you can't redirect your unauthenticated users at all, at some point. The best way to handle that is to catch the 401 response returned by the JWT middleware at the client level and redirect the user to the appropriate login page. In JS applications for instance, this is usually done using an HTTP interceptor.

If you're really convinced breaking the OAuth2 bearer specification is the right thing to do, you can do that using the OnChallenge notification:

app.UseJwtBearerAuthentication(options => {
    options.Events = new JwtBearerEvents {
        OnChallenge = context => {
            context.Response.Redirect("http://localhost:54540/login");
            context.HandleResponse();

            return Task.FromResult(0);
        }
    };
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!