我正在使用ASP.NET身份与几个外部登录提供程序,我需要处理以下方案:
1)用户使用外部服务登录(假设它是Facebook),应用程序从Facebook获取一些信息(姓名,电子邮件,出生日期等等).包含此信息的声明被添加到身份.
2)对于以下场景,我需要将此信息存储在应用程序Db中:
管理员浏览已注册用户的列表
电子邮件订阅服务将使用名字和姓氏
问题是如果用户将更新他/她的Facebook个人资料(例如,更改电子邮件地址) - 在这种情况下我需要更新我的Db中的信息(我在AspNetUserClaims表中存储外部声明).实际上,每次外部用户通过身份验证时我都需要更新它.
这是第一次如何保存它的代码:
扩展索赔类:
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
public string Issuer { get; set; }
public string ClaimValueType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
启动:
var facebookOptions = new FacebookAuthenticationOptions {
AppId = "...",
AppSecret = "...",
Provider = new FacebookAuthenticationProvider {
OnAuthenticated = (context) => {
context.Identity.AddClaims(new[] {
new Claim("LastName", context.User["last_name"].ToString(), ClaimValueTypes.String, "Facebook"),
new Claim("FirstName", context.User["first_name"].ToString(), ClaimValueTypes.String, "Facebook"),
//...Other claims
});
}
}
};
facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("user_birthday");
app.UseFacebookAuthentication(facebookOptions);
Run Code Online (Sandbox Code Playgroud)
AuthController
外部登录回调:
public ActionResult …Run Code Online (Sandbox Code Playgroud)