ASP.NET Core-推荐的自定义身份验证方法

时间:2018-06-21 11:32:26

标签: authentication asp.net-core

在此处阅读文档:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.authenticationservicecollectionextensions.addauthentication

然后遍历那些文档,选项类等中的所有扩展名和方法签名。似乎下面的(草稿)代码将用于设置我自己的自定义令牌身份验证。

# Make an options class called MyTokenOptions derive from AuthenticationSchemeOptions.
# Make my own handler and scheme classes (omitted) and new them up.

var handler = new MyTokenHandler();
var tokenScheme = new MyAuthenticationScheme("MyTokenV1", "My Encrypted Token v1", handler);

# Now configure my classes with ASP.NET Core.

services
    .AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = tokenScheme;
        })
    .AddScheme<MyTokenOptions,MyTokenHandler>("MyTokenV1", myOptions =>
        {
            myOptions.AllowAnonymous = false;
        });

但是,制作自己的中间件组件并从标头读取令牌,构建主体并设置httpContext.User = myClaimsPrincipal并返回,或者设置401并使管道短路似乎更简单。

我需要做所有文档中难以置信的复杂事情吗?

0 个答案:

没有答案