今天我尝试在我的项目上实现外部Facebook登录。 但是当我尝试登录调用ExternalLogin的方法时,此方法不会返回到Facebook登录页面
我试着这样做: https://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/
当我看到我的上下文时,AuthenticationResponseChallenge为空。
按照代码:
Startup.Auth.cs
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
var fao = new FacebookAuthenticationOptions
{
AppId = APP_ID_FACEBOOK, //on Web.config
AppSecret = APP_SECRET_FACEBOOK //on Web.config
};
fao.Scope.Add("email");
fao.Scope.Add("basic_info");
fao.Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
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);
}
};
fao.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
app.UseFacebookAuthentication(fao);
AccountController.cs
public ActionResult ExternalLogin(string provider, string returnUrl)
{
var result = new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Conta", new { ReturnUrl = returnUrl }));
return result;
}
private const string XsrfKey = "XsrfId";
internal class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUri)
: this(provider, redirectUri, null)
{
}
public ChallengeResult(string provider, string redirectUri, string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
public string LoginProvider { get; set; }
public string RedirectUri { get; set; }
public string UserId { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
}
我尝试使用我的Facebook应用凭据创建一个空项目并且工作正常,但在此项目中这不起作用
任何人都可以帮助我?
答案 0 :(得分:0)
一天之后,我找到了解决方案
我的webconfig是
<authentication mode="Forms">
<forms loginUrl="~/Login" name=".ASPXFORMSAUTH" />
</authentication>
当我改为
时<authentication mode="None" />
提交表格后,challeger开始重定向到facebook登录。