English 中文(简体)
如何使用Ausentication Filters in .NET Core
原标题:How to use Authentication Filters in .NET Core

之后是,该在制作的APICI中设置认证过滤器的指南。 这完全容易,除非在测试时,我获得这一例外。

No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions).

每当我试图打一个终点。

I ve look at this Guide,其中还规定了authenticationScheme 。 like

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme,
        options => builder.Configuration.Bind("JwtSettings", options))
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
        options => builder.Configuration.Bind("CookieSettings", options));

但我不想这样做。 是否有办法通过属性使用认证过滤器?

在这方面,我是如何安排的。

基本过滤器:

[AttributeUsage(AttributeTargets.All)]
public class BasicFilter : AuthorizeAttribute, IAuthenticationFilter
{        
    async public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
        BaseRequest? request = await AuthenticationHelper.RequestIsValid<BaseRequest>(context);
        if (request == null)
        {
            context.ErrorResult = new AuthenticationFailureResult(context.Request);
            return;
        }

        GenericPrincipal principal = new GenericPrincipal(new GenericIdentity("User"), null);
        context.Principal = principal;
    }

    public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
    {
        var challenge = new AuthenticationHeaderValue("None");
        context.Result = new ChallengeUnauthorizedResult(challenge, context.Result);

        return Task.FromResult(0);
    }
}

而随后又扔下了<条码>[巴塞尔代码]和<条码>[阿法明]等终点:

    [Authorize]
    [BasicFilter]
    [HttpPost]
    async public Task<ActionResult> CreateAccount()
    {
    }
问题回答

http://learn.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters”rel=“nofollow noreferer” 您提及的是 as-net 而不是p.net 核心数据。

在p.net芯中,我们可以授权过滤特性如下:

public  class CustomAuthorizeAttribute : Attribute, IAuthorizationFilter
 {
     public void OnAuthorization(AuthorizationFilterContext context)
     {
         if (context != null)
         {
             //wirte your logic here
         }
     }
 }

Notice although the build-in Authorization filter would call the build-in authorization system,custom authorization filters require a custom authorization framework,which means you have to read claims from jwt/cookie and validate them yourself

另外,请查看这一图表:

“在此处的影像描述”/</a

如果请求是坏的,或者没有在授权的过滤器中进行有约束力/示范性鉴定,那么你就不得不在行动基金会/PageFilter(RazorPage)中检查。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签