在ASP.NET Core 2中获取当前的身份验证方案 [英] Get current authentication scheme in ASP.NET core 2

查看:68
本文介绍了在ASP.NET Core 2中获取当前的身份验证方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力使用一个Asp.net core 2应用程序,该应用程序使用两个openid提供程序进行身份验证,并映射到两个不同的身份验证方案(具有不同的名称).

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).

我面临的问题是尝试注销当前正在使用的特定方案.例如,如果我同时支持Google和Facebook身份验证,则需要了解当前正在使用哪种方案,并调用 SignOut 方法以指示正确的方案.这使我可以清除本地cookie,还可以将用户重定向到外部身份提供者并注销.

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.

问题是我无法找到 GetCurrentScheme()之类的函数,因此无法在 SignOut 方法中用于指定方案.我确定我缺少基本的东西...

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...

推荐答案

似乎没有任何直接方法可以找到当前方案.这是一种粗略的方法:

There does not seem be any direct way to find current scheme. Here is a crude way:

private readonly IAuthenticationSchemeProvider authenticationSchemeProvider;
...
foreach(var scheme in authenticationSchemeProvider.GetRequestHandlerSchemesAsync()){
    var authResult = await context.AuthenticateAsync(scheme.Name);
    if(authResult.Succeeded)
        // this is the scheme that was used for authentication
}

这篇关于在ASP.NET Core 2中获取当前的身份验证方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆