Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current authentication scheme in ASP.NET core 2

I am currently struggling with an Asp.net core 2 application which uses two openid providers for authentication, mapped to two different Authentication Schemes (with different names).

The problem I am facing is trying to logout of the specific scheme that is currently being used. For example, if I support both Google and Facebook authentication, I need to understand which scheme is currently being used, and call the SignOut method indicating the correct scheme. This allows me to clear the local cookies and also redirect the user to the external identity provider and logout.

The thing is that I am not able to find a GetCurrentScheme() sort of function so that I can use to then specify the scheme in the SignOut method. I am sure I am missing something basic...

like image 475
LucaV Avatar asked Feb 22 '19 18:02

LucaV


People also ask

What is authentication scheme .NET core?

Authentication is the process of determining a user's identity. Authorization is the process of determining whether a user has access to a resource. In ASP.NET Core, authentication is handled by the authentication service, IAuthenticationService, which is used by authentication middleware.

How many types of authentication are there in ASP.NET Core?

This blog starts with authentication and authorization concepts and after that explains the three default important ways and three custom authentication ways for doing authentication and authorization i.e. windows, forms ,passport, multipass, JWT and SAML authentication.

What is the authentication scheme?

An authentication scheme is a module that implements a way for a user to authenticate itself to SimpleID. In particular, an authentication scheme checks credentials presented by the user against some data store containing user information, and determines whether the credentials match those stored in the data store.

What is authentication scheme in ASP NET Core?

For an introduction to authentication schemes in ASP.NET Core, see Authentication scheme. In some scenarios, such as Single Page Applications (SPAs), it's common to use multiple authentication methods. For example, the app may use cookie-based authentication to log in and JWT bearer authentication for JavaScript requests.

How do I authorize an app in ASP NET Core?

Select the handler with which the app will authorize by passing a comma-delimited list of authentication schemes to [Authorize]. The [Authorize] attribute specifies the authentication scheme or schemes to use regardless of whether a default is configured. For example: ASP.NET Core 2.x.

What is the use of authentication policy in ASP NET?

Authentication policy schemes make it: Easy to forward any authentication action to another scheme. Forward dynamically based on the request. All authentication schemes that use derived AuthenticationSchemeOptions and the associated AuthenticationHandler<TOptions>: Are automatically policy schemes in ASP.NET Core 2.1 and later.

What is the use of ASP NET authenticator middleware?

Authentication middleware is responsible for authentication in ASP.Net Core applications. The authentication middleware uses the registered authentication handlers to authenticate a user. The registered handlers and their associated configurations are called schemes.


1 Answers

I had the same question, but I finally put the authentication scheme in the claims collection in my SignIn method :

claims.Add(new Claim(ClaimTypes.AuthenticationMethod, authenticationScheme));

So, in the SignOut method, I can retrieve the authentication scheme :

var authenticationScheme = HttpContext.User.FindFirstValue(ClaimTypes.AuthenticationMethod);
like image 178
Kakone Avatar answered Sep 30 '22 13:09

Kakone