我们有一个全新的ASP.NET Core网站,目的是升级旧的ASP.NET Framework网站。那时我们使用DotNetOpenAuth进行OpenID登录,现在我们正尝试在ASP.NET Core中进行复制。
我们已经可以将网站重定向到OpenID提供程序,并且能够登录,但是在回拨到我们的网站时会引发异常:
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultSignInScheme found.
我找不到完整的示例,这确实有助于我理解整个过程。我从各种来源整理了以下内容...
在启动公司的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
内的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("~/");
}
我什至可以在异常数据中看到,我们正在获取必要的登录信息,但是我确实需要一个完整的示例以使我能够理解。