我不明白为什么他们不是一个明确的教程或指导,所以我希望我的问题可以在这里得到解答。注册外部登录Web API
因此,试图通过Web Api注册Facebook或谷歌用户。
的问题是,在RegisterExternal
方法,在这条线:
var info = await Authentication.GetExternalLoginInfoAsync();
它返回null,从而返回BadRequest()
我得到了什么至今:
在Startup.Auth.cs
我已经知道了身份证和秘密,请注意,我也试过使用Microsoft.Owin.Security.Facebook
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
{
AppId = "103596246642104",
AppSecret = "1c9c8f696e47bbc661702821c5a8ae75",
Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, ClaimValueTypes.String, "Facebook"));
return Task.FromResult(0);
}
},
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "328779658984-t9d67rh2nr681bahfusan0m5vuqeck13.apps.googleusercontent.com",
ClientSecret = "ZYcNHxBqH56Y0J2-tYowp9q0",
CallbackPath = new PathString("/api/Account/ManageInfo")
});
facebookOptions来源:this post
这额外facebookOptions没有解决的问题。
我可以从Google和Facebook上检索access_token。我也能与此的access_token来验证api/Account/UserInfo
GET http://localhost:4856/api/Account/UserInfo
in the header:
Authorization: Bearer R9BTVhI0...
将返回: {"Email":"firstname lastname","HasRegistered":false,"LoginProvider":"Facebook"}
一个问题,我注意到了,就是它返回我的名字是电子邮件,而不是实际的电子邮件地址。
现在我想为我的数据库中的新用户,这是我做一个POST调用像这样来注册外部登录:
POST http://localhost:4856/api/Account/RegisterExternal
[header]
authorization: bearer 6xcJoutY...
Content-Type: application/json
[body]
{"Email":"[email protected]"}
来源:this post
现在这个返回一个错误请求在此代码片段,里面RegisterExternal():
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//AuthenticationManger?
var info = await Authentication.GetExternalLoginInfoAsync();
if (info == null)
{
return InternalServerError();
}
在调试时,ExternalLoginConfirmationViewModel
确实包含我的电子邮件地址。
我在做什么错?我需要在Startup.cs
上添加什么吗? Startup.Auth.cs
还有什么我需要做的吗?我错误地打电话给RegisterExternal
吗?在MVC中它非常流畅,为什么不在Web API中?
阿苏看着this answerthis question,但我不明白如何实现这一点。
当时这个不断解决?我有同样的问题。 –
最后,我在应用程序端使用了Facebook进行身份验证,就像在开发应用程序时一样。然后使用外部访问令牌在我的API上注册我的用户。遵循本教程:http://bitoftech.net/2014/08/11/asp-net-web-api-2-external-logins-social-logins-facebook-google-angularjs-app/如果您有第四个外部访问令牌,您可以使用它来使用facebook图表API获取用户信息。这是在android的这个答案:http://stackoverflow.com/questions/31314124/get-email-and-name-facebook-sdk-v4-4-0-swift – CularBytes
非常感谢,非常有帮助。 –