我正在尝试使用标准的mvc.net外部身份验证进行身份验证后从Twitter检索配置文件信息。
我可以使用位于ExternalLoginCallback函数中的以下代码(需要安装Facebook SDK)为Facebook做这个。
if (string.Equals(loginInfo.Login.LoginProvider, "facebook", StringComparison.CurrentCultureIgnoreCase))
{
var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
var access_token = identity.FindFirstValue("FacebookAccessToken");
var fb = new FacebookClient(access_token);
// you need to specify all the fields that you want to get back
dynamic myInfo = fb.Get("/me?fields=email,first_name,last_name");
email = myInfo.email;
firstName = myInfo.first_name;
lastName = myInfo.last_name;
}
我正在寻找相当于推特的推特。
TIA
所以这就是我在很大程度上基于此做的
Obtain twitter access token asp.net identity
首先,我根据上面的@Mate建议安装了TwitterinviAPI nuget包。
其次,我在StartUp.Auth.cs中更新了ConfigureAuth的twitter部分,以包含“Provider”
app.UseTwitterAuthentication(new TwitterAuthenticationOptions()
{
ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
Provider = new LinqToTwitterAuthenticationProvider(),
BackchannelCertificateValidator = new Microsoft.Owin.Security.CertificateSubjectKeyIdentifierValidator(new[]
{
"A5EF0B11CEC04103A34A659048B21CE0572D7D47", // VeriSign Class 3 Secure Server CA - G2
"0D445C165344C1827E1D20AB25F40163D8BE79A5", // VeriSign Class 3 Secure Server CA - G3
"7FD365A7C2DDECBBF03009F34339FA02AF333133", // VeriSign Class 3 Public Primary Certification Authority - G5
"39A55D933676616E73A761DFA16A7E59CDE66FAD", // Symantec Class 3 Secure Server CA - G4
"add53f6680fe66e383cbac3e60922e3b4c412bed", // Symantec Class 3 EV SSL CA - G3
"4eb6d578499b1ccf5f581ead56be3d9b6744a5e5", // VeriSign Class 3 Primary CA - G5
"5168FF90AF0207753CCCD9656462A212B859723B", // DigiCert SHA2 High Assurance Server CA
"B13EC36903F8BF4701D498261A0802EF63642BC3" // DigiCert High Assurance EV Root CA
}),
});
第三,我将LinqToTwitterAuthenticationProvider类添加到Model / IdentityModels中(尽管您可以将此代码放在任何您喜欢的位置)
public class LinqToTwitterAuthenticationProvider : TwitterAuthenticationProvider
{
public const string AccessToken = "TwitterAccessToken";
public const string AccessTokenSecret = "TwitterAccessTokenSecret";
public override Task Authenticated(TwitterAuthenticatedContext context)
{
context.Identity.AddClaims(
new List<Claim>
{
new Claim(AccessToken, context.AccessToken),
new Claim(AccessTokenSecret, context.AccessTokenSecret)
});
return base.Authenticated(context);
}
最后,我使用以下代码更新了Accounts控制器中的ExternalLoginCallback,以便从twitter获取用户详细信息
else if (string.Equals(loginInfo.Login.LoginProvider, "twitter", StringComparison.CurrentCultureIgnoreCase))
{
// Generate credentials that we want to use
var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
var access_token = identity.FindFirstValue(LinqToTwitterAuthenticationProvider.AccessToken);
var access_token_secret = identity.FindFirstValue(LinqToTwitterAuthenticationProvider.AccessTokenSecret);
var creds = new TwitterCredentials(ConfigurationManager.AppSettings["TwitterConsumerKey"], ConfigurationManager.AppSettings["TwitterConsumerSecret"],access_token, access_token_secret);
var authenticatedUser = Tweetinvi.User.GetAuthenticatedUser(creds);
email = authenticatedUser.Email;
firstName = authenticatedUser.Name.Substring(0, authenticatedUser.Name.IndexOf(' '));
lastName = authenticatedUser.Name.Substring(authenticatedUser.Name.IndexOf(' ') + 1);
}
有一个小GOTCHYA。即使在所有这些之后,电子邮件仍然是null。问题是在我的Twitter应用程序设置中我没有提供隐私或ToS URL,因此无法检查权限选项卡下的选项以请求用户发送电子邮件。只是一个友好的FYI。
希望这可以帮助!!