我想通过外部提供商(如facebook)注册用户,以获取我需要的信息,我按照以下方式配置FacebookProvider
var options = new FacebookAuthenticationOptions {
AppId = "***",
AppSecret = "***",
Scope = { "email" },
Provider = new FacebookAuthenticationProvider {
OnAuthenticated = (context) => {
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"));
}
return Task.FromResult(0);
}
}
};
options.Fields.Add("id");
options.Fields.Add("name");
options.Fields.Add("email");
options.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalBearer;
app.UseFacebookAuthentication(options);
在调试时OnAuthenticated中的我看到所有请求的字段,但是当我从邮递员调用RegisterExternal时如下图
GetExternalLoginInfoAsync返回null
var info = await Authentication.GetExternalLoginInfoAsync();
if (info == null)
{
return InternalServerError();
}
那么如何检索电子邮件等查询字段?我认为所有必要的信息都存储在cookie中,但是如何将它们传输到服务器并提取Identity实例呢?
所有nuget包已更新至最新版本
P.S。我计划使用iOS应用程序中的API
答案 0 :(得分:0)
我找到了解决方案。
更改 ExternalLoginData 类,如下所示
private class ExternalLoginData
{
...
// here added new field
public IList<Claim> Claims { get; private set; }
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
...
return new ExternalLoginData
{
...
// here added claims setting
Claims = identity.Claims.ToList()
};
}
}
更改了 ExternalLogin 回调,如下所示
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
...
if (hasRegistered)
{
...
}
else
{
// here replaced getting claims by Claims field
IEnumerable<Claim> claims = externalLogin.Claims;
//IEnumerable<Claim> claims = externalLogin.GetClaims();
ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
Authentication.SignIn(identity);
}
return Ok();
}
结果我们收到了一个不记名令牌。从中提取身份我们收到之前保存的声明。