我正在创建一个仅使用Facebook / Google身份验证的ASP.NET 5 MVC 6应用。我还试图在没有整个ASP.NET标识的情况下使用cookie中间件 - 遵循这篇文章: https://docs.asp.net/en/latest/security/authentication/cookie.html
所以我开始使用没有身份验证的空白应用程序然后添加了Microsoft.AspNet.Authentication.Cookies和Microsoft.AspNet.Authentication.Facebook NuGet包,以便采用非常简约的方法,我不会包含任何内容我不需要。
我将以下代码添加到Startup.cs中的Configure中,但是我必须提供"必须提供SignInScheme选项"错误。知道我错过了什么吗?
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "MyCookieMiddlewareInstance";
options.LoginPath = new PathString("/Accounts/Login/");
options.AccessDeniedPath = new PathString("/Error/Unauthorized/");
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
});
app.UseFacebookAuthentication(options =>
{
options.AppId = "myFacebookAppIdGoesHere";
options.AppSecret = "myFacebookAppSecretGoesHere";
});
答案 0 :(得分:9)
如您所看到的错误消息所示,您需要在Facebook中间件选项中设置options.SignInScheme
:
app.UseFacebookAuthentication(options => {
options.AppId = "myFacebookAppIdGoesHere";
options.AppSecret = "myFacebookAppSecretGoesHere";
// This value must correspond to the instance of the cookie
// middleware used to create the authentication cookie.
options.SignInScheme = "MyCookieMiddlewareInstance";
});
或者,您也可以从ConfigureServices
全局设置它(它将配置每个身份验证中间件,因此您不必设置options.SignInScheme
):
public void ConfigureServices(IServiceCollection services) {
services.AddAuthentication(options => {
// This value must correspond to the instance of the cookie
// middleware used to create the authentication cookie.
options.SignInScheme = "MyCookieMiddlewareInstance";
});
}