问题描述
似乎有很多关于如何得到使用ASP.NET身份和MVC客户端从Facebook的个人主页的其他信息的文档,但我似乎无法找到如何访问附加信息从任何索赔网页API控制器。
There appears to be a lot of documentation on how to get additional information from a Facebook profile using ASP.NET Identity and an MVC client, but I can't seem to find anything on how to access the additional info claims from a Web API controller.
我Startup.Auth.cs ConfigAuth方法中包含这一点,这似乎如果我断点上好吗工作JObject wholeUser = context.User
My Startup.Auth.cs ConfigAuth method contains this, which seems to work alright if I breakpoint on JObject wholeUser = context.User
String XmlSchemaString = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims";
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = "*",
AppSecret = "*",
Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
JObject wholeUser = context.User;
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:email", context.Email, XmlSchemaString, "Facebook"));
return Task.FromResult(0);
}
}
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);
一旦通过验证,wholeUser将有我需要像生日等,但一旦出了这个范围的所有信息,我不知道如何得到它。
Once authenticated, wholeUser will have all the information I need like birthdate, etc. but once out of this scope, I don't know how to get to it.
我需要在AccountController.RegisterExternal这些信息,但是我似乎无法正确配置。很多MVC DOCO周围使用这个在ExternalLoginCallback方法:
I need to get this information in AccountController.RegisterExternal, but I can't seem to configure it correctly. A lot of the MVC doco around uses this in the ExternalLoginCallback method:
ClaimsIdentity claimsIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
但AuthenticationManager会不能在网络API中找到,所以如果我改变它只是验证,它,除非我改变的参数是DefaultAuthenticationTypes.ExternalBearer返回null。我得到这是接近,但返回ClaimsIdentity对象没有任何额外的索赔 - 只需用户名和ID
But AuthenticationManager can't be found in Web API, so if I change it to just Authentication, it returns null unless I change the parameter to be DefaultAuthenticationTypes.ExternalBearer. This is as close as I get, but the returned ClaimsIdentity object doesn't have any of the additional claims - just the username and ID.
所以,总体来说,这是因为虽然我从Facebook获得的信息正确,并进入恶补context.Identity,但我不知道如何从控制器访问它。 N.B:在ConfigAuth上下文对象的类型是Microsoft.Owin.Security.Facebook.FacebookAuthenticatedContext的
So overall, it's as though I'm getting the information from Facebook properly and cramming into context.Identity, but then I have no idea how to access it from the controller. N.B: the context object in ConfigAuth is of type Microsoft.Owin.Security.Facebook.FacebookAuthenticatedContext
推荐答案
我用下面的code在这里;
I have used the following code here;
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
{
AppId = "*",
AppSecret = "**",
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);
现在在RegisterExternal方法,索赔是提供给我。我只是无法弄清楚如何坚持它的身份,以便它可以在其他控制器调用中使用。如果我的索赔增加的身份,然后它似乎打破其他调用User.Identity对象;
Now in the RegisterExternal method, the claim is available to me. I just can't figure out how to persist it in the Identity so that it can be used in other Controller calls. If i add the claim to the identity then it seems to break the User.Identity object on other calls;
ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);
而FromIdentity方法;
And the FromIdentity method;
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
string userEmail = string.Empty;
string userFacebookAccessToken = string.Empty;
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
Claim facebookToken = identity.Claims.First(x => x.Type.Contains("access_token"));
Claim emailDetails = identity.FindFirst(ClaimTypes.Email);
if (facebookToken != null) userFacebookAccessToken = facebookToken.Value;
if (emailDetails != null) userEmail = emailDetails.Value;
//string userEmail = identity.Claims.First(x => x.Type.Contains("emailaddress")).Value;
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer)
|| String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Name),
Email = userEmail,
FacebookAccessToken = userFacebookAccessToken
};
}
}
这篇关于外部登录过程中检索来自Facebook的其他资料信息/注册的ASP.NET Web API 2.0和身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!