我已成功使用以下教程进行 Facebook 注册和登录。
我还在 Facebook 应用程序开发网站上阅读了有关姓名和电子邮件等详细信息请求权限的信息...
https://developers.facebook.com/docs/facebook-login/permissions
与此相关的是,我在 Facebook 开发者中心的“应用详细信息”页面上找到了“配置应用中心权限”按钮。我能够根据请求的许可添加“电子邮件”。据我了解,名字和姓氏可以从公开个人资料中获得。
我不知道这些设置需要多长时间才能过滤掉,但是当我注册 Facebook 时,它说它只请求我的公开个人资料和 friend 列表,没有电子邮件。
Razor 代码
我的外部登录部分 View 中的 Razor 代码如下所示(项目模板中的标准 View ...HTML 已被删除)...
@using Microsoft.Owin.Security
...
@{
var 登录提供商;
var context = Context.GetOwinContext();
LoginProviders = context.Authentication.GetExternalAuthenticationTypes();
if (loginProviders.Count() > 0)
{
字符串操作 = Model.Action//ExternalLogin;
字符串 returnUrl = Model.ReturnUrl;
使用 (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl }))
{
@Html.AntiForgeryToken()
@foreach(loginProviders 中的 AuthenticationDescription p)
{
@* 使用 p.AuthenticationType 创建提交按钮
}
}
}
}
帐户 Controller
处理此请求的帐户 Controller 代码是(“提供者”是 Facebook,它是表单中的 AuthenticationType):
<前>[HttpPost]
[允许匿名]
[验证防伪 token ]
公共(public)ActionResultExternalLogin(字符串提供者,字符串returnUrl)
{
返回新的挑战结果(
提供者,
Url.Action("ExternalLoginCallback", "帐户", new { ReturnUrl = returnUrl }));
}
帐户 Controller - ChallengeResult
私有(private)类 ChallengeResult : HttpUnauthorizedResult
{
公开挑战结果(
字符串提供者,
字符串重定向Uri)
:这个(提供商,redirectUri,空)
{
}
公开挑战结果(
字符串提供者,
字符串重定向Uri,
字符串用户 ID)
{
this.LoginProvider = 提供商;
this.RedirectUri = redirectUri;
this.UserId = 用户Id;
}
公共(public)字符串 LoginProvider { 获取;放; }
公共(public)字符串 RedirectUri { 获取;放; }
公共(public)字符串用户ID { 获取;放; }
公共(public)覆盖无效执行结果(ControllerContext上下文)
{
var 属性 = new AuthenticationProperties() { RedirectUri = RedirectUri };
if (用户ID!=空)
{
属性.Dictionary["XsrfId"] = UserId;
}
var authContext = context.HttpContext.GetOwinContext();
authContext.Authentication.Challenge(属性,LoginProvider);
}
}
我感觉 ChallengeResult 类中需要做一些事情。也许添加额外的 AuthenticationProperties。
有人知道我错过了什么吗?
最佳答案
如果您在 MVC 5 中的 FacebookAuthenticationOptions 范围中添加“电子邮件”条件,它会请求用户的电子邮件权限。您没有发布 Startup.Auth.cs,但我认为您使用了默认的 Facebook 身份验证。
尝试使用自定义 FacebookAuthentication 选项,如下所示:
Startup.Auth.cs
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//Customizing FacebookAuthenticationOptions...
var facebookOptions=new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = "***",
AppSecret = "***"
};
facebookOptions.Scope.Add("email"); //We want to get user's email information by adding it in scope.
app.UseFacebookAuthentication(facebookOptions);
关于asp.net-mvc - 在 ASP.Net MVC (Microsoft.AspNet.Identity.EntityFramework) 中进行 Facebook 注册时获取姓名和电子邮件详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22874606/