2017年更新!
我在发布原始问题时遇到的问题与Facebook最近在强迫每个人使用其API版本2.3时所做的更改无关.有关该特定问题的解决方案,请参阅下面的sammy34的答案./ oauth/access_token端点的2.3版现在返回JSON而不是表单编码的值
由于历史原因,这是我原来的问题/问题:
我有一个MVC5 Web应用程序,它使用内置支持通过Facebook和Google进行身份验证.几个月前我们构建这个应用程序时,我们遵循了这个教程:http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and -google-oauth2-and-openid-sign-on,一切都很棒.
现在,突然之间,Facebook身份验证刚刚停止工作.Google身份验证仍然有效.
问题描述:我们点击链接使用Facebook连接,我们被重定向到Facebook,如果我们不允许我们的Facebook应用访问我们的个人资料,我们会被提示.当我们点击"确定"时,我们会被重定向回我们的网站,但我们只是登录屏幕而不是登录.
我已经在调试模式下完成了这个过程,并且根据上面提到的教程,我在我的帐户控制器中有了这个ActionResult:
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
............
Run Code Online (Sandbox Code Playgroud)
当单步执行代码并从Facebook返回时,loginInfo对象始终为NULL,这会导致用户重定向回登录.
为了理解幕后实际发生的事情,我安装了Fiddler并监控HTTP流量.我发现的是,在Facebook权限对话框中单击"确定"后,Facebook会使用以下URL重定向回我们的应用程序:
https://localhost/signin-facebook?code=<access-token>
Run Code Online (Sandbox Code Playgroud)
这个URL不是一个实际的文件,可能是由我猜测的这个OWIN框架内置的一些控制器/处理程序处理的.最有可能的是,它使用给定的代码连接回Facebook,以查询有关尝试登录的用户的信息.现在,问题是我们被重定向到:而不是这样做:
/Account/ExternalLoginCallback?error=access_denied
Run Code Online (Sandbox Code Playgroud)
我确信这是Facebook正在做的事情,也就是说,它不是向我们提供用户数据,而是通过此错误消息重定向我们.
这会导致AuthenticationManager.GetExternalLoginInfoAsync();失败并始终返回NULL.
我完全没有想法.据我们所知,我们没有改变任何事情.
我已经尝试创建一个新的Facebook应用程序,我已经尝试过再次使用该教程,但我总是遇到同样的问题.
欢迎任何想法!
更新!
好的,这让我疯了!我现在已经手动完成了执行身份验证所需的步骤,当我这样做时,一切都很顺利.为什么在使用MVC5 Owin的东西时这不起作用?
这就是我做的:
// Step 1 - Pasted this into a browser, this returns a code
https://www.facebook.com/dialog/oauth?response_type=code&client_id=619359858118523&redirect_uri=https%3A%2F%2Flocalhost%2Fsignin-facebook&scope=&state=u9R1m4iRI6Td4yACEgO99ETQw9NAos06bZWilJxJrXRn1rh4KEQhfuEVAq52UPnUif-lEHgayyWrsrdlW6t3ghLD8iFGX5S2iUBHotyTqCCQ9lx2Nl091pHPIw1N0JV23sc4wYfOs2YU5smyw9MGhcEuinvTAEql2QhBowR62FfU6PY4lA6m8pD3odI5MwBYOMor3eMLu2qnpEk0GekbtTVWgQnKnH6t1UcC6KcNXYY
I was redirected back to localhost (which I had shut …Run Code Online (Sandbox Code Playgroud) 我正在尝试检索作为OnAuthenticated上下文返回的用户属性,并在此示例后添加为声明: 如何使用ASP.NET身份(OWIN)访问Facebook私人信息?
我可以看到我期望的数据是在登录时返回的,并且在Starup.Auth.cs中被添加为声明.但是,当我在帐户控制器内时,UserManager或UserStore中出现的唯一声明由LOCAL AUTHORITY颁发.Facebook(或其他外部提供商)无法找到任何索赔.在上下文中添加的声明到底在哪里?(我正在使用VS2013 RTM.)
Azure上的完整源代码和实时站点链接到这里:https://github.com/johndpalm/IdentityUserPropertiesSample/tree/VS2013rtm
这是我在Startup.Auth.cs中的内容:
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = ConfigurationManager.AppSettings.Get("FacebookAppId"),
AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"),
Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
foreach (var x in context.User)
{
var claimType = string.Format("urn:facebook:{0}", x.Key);
string claimValue = x.Value.ToString();
if (!context.Identity.HasClaim(claimType, claimValue))
context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook"));
}
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
return Task.FromResult(0);
}
}
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);
Run Code Online (Sandbox Code Playgroud)
捕获外部登录属性的另一种方法是为访问令牌添加单个声明并使用属性填充它:
const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
var …Run Code Online (Sandbox Code Playgroud) 将Microsoft.AspNet.Identity.Core升级到1.0.0-rc1后,AccountController.cs和AppModel.cs类中出现了几个错误,这些错误由Visual Studio 2013中的默认MVC5模板生成.
是否有任何发行说明来解释如何解决重大变化?
entity-framework-6 visual-studio-2013 asp.net-mvc-5 asp.net-identity
我正在学习如何使用VS 2015中的默认MVC模板使用OWIN外部auth.我启用了Facebook auth并添加了范围"email",期望在用户通过身份验证后由Facebook返回用户的电子邮件(根据此) .但是,上下文中没有电子邮件.用户JSON对象和context.Email也为空.
这是Startup.Auth.cs中的相关代码
var facebookOptions = new FacebookAuthenticationOptions
{
AppId = "XXXXXX",
AppSecret = "XXXXXX",
Provider = new FacebookAuthenticationProvider
{
OnAuthenticated = context =>
{
// Retrieve the OAuth access token to store for subsequent API calls
var accessToken = context.AccessToken;
// Retrieve the username
var facebookUserName = context.UserName;
// WHY IS IT EMPTY?
var facebookEmail = context.Email;
// You can even retrieve the full JSON-serialized user
var serializedUser = context.User;
return Task.FromResult(0);
}
}
}; …Run Code Online (Sandbox Code Playgroud) asp.net-mvc facebook-graph-api owin asp.net-mvc-5 asp.net-identity