c# - ASP.NET Core: InvalidOperationException: 没有指定 authenticationScheme,也没有找到 DefaultSignInScheme
问题描述
我们有一个全新的 ASP.NET Core 站点,用于升级旧的 ASP.NET Framework 站点。那时我们使用 DotNetOpenAuth 进行 OpenID 登录,现在我们尝试在 ASP.NET Core 中进行复制。
我们已经将我们的站点重定向到 OpenID 提供程序,能够登录,但是在回调我们的站点时会引发异常:
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultSignInScheme found.
我找不到一个完整的例子,这真的可以帮助我理解整个过程。我从各种来源收集了以下内容......
在 Startup 的 ConfigureServices 中:
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always; //require https
// Make the session cookie essential
options.Cookie.IsEssential = true;
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false; //true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSimpleInjector(_simpleInjectorContainer, options =>
{
// AddAspNetCore() wraps web requests in a Simple Injector scope.
options.AddAspNetCore()
// Ensure activation of a specific framework type to be created by
// Simple Injector instead of the built-in configuration system.
.AddControllerActivation()
.AddViewComponentActivation();
//.AddPageModelActivation()
//.AddTagHelperActivation();
});
services.AddAuthentication(options =>
{ /* Authentication options */
//options.DefaultAuthenticateScheme = "Steam";
})
.AddSteam(options =>
{
});
由于之前没有 ASP.NET Core 的经验,我盲目地尝试将“Steam”分配给DefaultAuthenticateScheme
inside of AddAuthentication
,但这会引发错误,说它不能调用自身。
我们一直在使用默认的 Home 控制器作为测试场:
[HttpGet("~/signin")]
public IActionResult SignIn()
{
// Instruct the OIDC client middleware to redirect the user agent to the identity provider.
// Note: the authenticationType parameter must match the value configured in Startup.cs
return Challenge(new AuthenticationProperties
{
RedirectUri = Url.Action("HandleSteamLogin", "Home"),
}, "Steam");
}
public async Task<IActionResult> HandleSteamLogin()
{
//Everything in this method is marked as obsolete, so it's a poor example. I guess it's from an older version of ASP.NET Core?
var claimsPrincipal = await HttpContext.Authentication.AuthenticateAsync("ExternalCookie");
//do something the the claimsPrincipal, possibly create a new one with additional information
//create a local user, etc
await HttpContext.Authentication.SignInAsync("MainCookie", claimsPrincipal);
await HttpContext.Authentication.SignOutAsync("ExternalCookie");
return Redirect("~/");
}
我什至可以在异常数据中看到,我们正在获取必要的登录信息,但我确实需要一个完整的示例才能理解。
解决方案
推荐阅读
- flutter - 如何在验证器中引用表单字段的值或颤振中的另一个表单字段
- c# - 单击的项目不播放歌曲并给出错误“系统找不到指定的文件”
- python - 从矩阵的 3D 堆栈在 numpy/scipy 中构造块对角矩阵的 3D 堆栈的有效方法
- c++ - gcc 模棱两可的模板实例化错误
- mapbox - Mapbox GL JS 忽略属性表达式
- android - 如何在Android上将多个http请求和数据库查询与Rxjava结合起来
- php - 如何验证指定的代理是否在 php 脚本中使用?
- iis-7 - 与 IP 地址关联的别名
- javascript - 反应钩子,回调中未捕获的变量
- c++ - 让编译器告诉哪些确切的纯虚方法使结构抽象?