【发布时间】:2018-12-03 15:29:01
【问题描述】:
所以,我一直在尝试在 ASP.NET WebApi 2 应用程序中实现使用 Facebook 登录的功能。
但由于某种原因,当我尝试验证匿名请求时:
if (!User.Identity.IsAuthenticated)
{
return new ChallengeResult(provider, this);
}
User 为空。
我已经在我的web.config 上将身份验证模式设置为None。
Web.config:
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<customErrors mode="Off" />
</system.web>
另外,这也是我在 Startup.cs 类中为 OAuth 配置管道的方式:
public void ConfigureOAuth(IAppBuilder app) {
app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
facebookAuthOptions = new FacebookAuthenticationOptions() {
AppId = "xxxxx",
AppSecret = "xxxxxxxxxxx",
Provider = new FacebookAuthProvider()
};
app.UseFacebookAuthentication(facebookAuthOptions);
}
这就是我获得控制器的方式:
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
[HttpGet]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null) {
string redirectUri = string.Empty;
// HERE: User is always null.
if (!User.Identity.IsAuthenticated) {
return new ChallengeResult(provider, this);
}
// Rest of the code removed for brevity...
}
据我所知,User.Identity 应该在 cookie 中包含登录提供程序返回的数据/声明,但用户返回 null;因此,不会触发挑战。
【问题讨论】:
标签: c# asp.net asp.net-web-api2 owin