我不明白爲什麼他們不是一個明確的教程或指導,所以我希望我的問題可以在這裏得到解答。註冊外部登錄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
非常感謝,非常有幫助。 –