【发布时间】:2017-01-10 16:56:08
【问题描述】:
我是 OWIN 的新手。目前我正在使用 Visual Studio 2015 创建 ASP.NET MVC 项目,Visual Studio 向导处理几乎基本功能,如注册新用户、使用外部帐户登录...... 在 Startup.Auth.cs 中,我使用 facebook 功能实现了登录,如下所示:
var option = new FacebookAuthenticationOptions
{
AppId = appId,
AppSecret = appSecret,
Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (ctx) =>
{
ctx.Identity.AddClaim(new System.Security.Claims.Claim("fb_access_token", ctx.AccessToken, ClaimValueTypes.String, "Facebook"));
return Task.FromResult(0);
}
},
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie
};
app.UseFacebookAuthentication(option);
在 AccountController.cs 中我想获取访问令牌以获取一些用户的信息,但我总是收到声明对象的空引用异常:
public ActionResult Test()
{
ClaimsIdentity claimIdenties = HttpContext.User.Identity as ClaimsIdentity;
var claim = claimIdenties.FindFirst("fb_access_token");
return View(claim.Value);
}
你们能建议我任何解决方案吗?
对不起,我英语不好
【问题讨论】:
标签: c# asp.net facebook asp.net-mvc-5 owin