.Net core 3.1 custom authentication

Fakhar Ahmad Rasul

I writing a .Net core API and my API itself does not have any authentication or authorization logic. The authentication and authorization are handled by a separate system so in order to secure my endpoints I need to forward each incoming request to the external system and decide if the user is authenticated depending upon the value being returned from the external system.

I want to know whats the best approach to implement something like this, as I think it can be done with a CustomAuthorize attribute or middleware or by adding a CustomAuthPolicy. There are many bits and pieces in Auth with .Net core, I would really appreciate if someone can guide me in the right direction.

Marius

Here is my solution that I am currently using:

This solution works great, and if you have the following in a referenced project, it can easily be reused.

Create a Custom Attribute:

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAuthAttr : ActionFilterAttribute
{
    //Add Auth logic here using HttpClient or whatever you use to authenticate.
    //You can access your headers through actionContext.HttpContext.Request.Headers


    //When completed with your logic, you can continue your controller execution
    base.OnActionExecuting(actionContext);
}

This attribute can be applied to your controllers like this:

[MyAuthAttr]
public class MySecureControllerController {...}

This can be applied to the Controller's class as global auth, or any of the endpoints within the controller as specific auth.

So this will work too:

[Route("Do")]
[MyAuthAttr]
public IActionResult DoThaThing(Foo foo) {...}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Custom Role with .NET Core 3 Azure AD Authentication

Custom Authentication in ASP.Net-Core

Custom Authentication mechanism in ASP.Net Core

ASP.NET Core 2.0 Preview 1: How to set up Cookie Authentication with custom login path

Asp.net Core Persistent Authentication - Custom Cookie Authentication

.NET Core 3 Cookie Authentication not setting identity

JWT Authentication using a custom attribute in .NET Core Web API

Custom Authentication using legacy user table in Asp.Net Core

Custom authentication asp.net core web api

Authorization failing for custom authentication handler for ASP.NET Core 3.1?

Using the Authorize Attribute with Custom Cookie Authentication in ASP.NET Core

.Net Core JWT Authentication with custom API Key Middleware

ASP.NET Core JWT with Custom Authentication Type

Custom API Key Authentication method in .net core not working on controller

Implementing A Custom 2 Factor Authentication Mechanism In Asp.Net Core

User.Identity.IsAuthenticated always false in .net core custom authentication

Custom Authentication & Update Claims in ASP.NET CORE

ASP.NET Core 2.1 Custom RoleProvider with Windows Authentication

.Net Core Custom Authentication using API Keys with Identity Server 4

Custom routing in .NET Core 3 MVC

.net identity custom authentication

.net Core Authentication

How to implement Custom UserStore (or custom authentication) in ASP.NET Core (not older versions!)

Is it possible to use custom RequireAuthorization and AllowAnonymous for custom middleware authentication in ASP Net Core 6 Minimal APIs?

.Net Core Authentication and user session

.Net Core HttpClient Digest Authentication

Firebase Authentication (JWT) with .NET Core

Authentication in .net Core with ReactJS SPA

.NET Core Token authentication - body