asp.net-mvc – MVC 5 Owin Facebook Auth导致空参考异常
发布时间:2020-12-15 18:41:04 所属栏目:asp.Net 来源:网络整理
导读:我试图在Visual Studio 2013中的一个新的MVC 5项目中设置集成的OWIN Facebook身份验证。我已经按照本教程配置了应用和密钥: http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on 但是
我试图在Visual Studio 2013中的一个新的MVC 5项目中设置集成的OWIN Facebook身份验证。我已经按照本教程配置了应用和密钥:
http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on 但是,我在AccountController中从此调用中抛出NullReferenceException: [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 我已经检查了Fiddler的回应,并且得到了Facebook的成功回应,但仍然收到这个错误。响应如下所示: {"id":"xxx","name":"xxx","first_name":"xxx","last_name":"xxx","link": "https://www.facebook.com/profile.php?id=xxx","location":{"id":"xxx","name":"xxx"},"gender":"xxx","timezone":1,"locale":"en_GB","verified":true,"updated_time":"2013-10-23T10:42:23+0000"} 我在http和https调试时得到这个。我猜这是一个框架错误,但是迄今为止已经通过反射器来绘制了一个空白的诊断。 解决方法
这可能是身份OWIN扩展代码中的错误。我无法重现这个问题,因为我的Facebook有效载荷总是返回一个用户名字段在json,这是从你的fb响应中丢失。我不太清楚为什么不在那里
身份owin扩展方法中的代码对于身份的名称声明没有空值检查,与用户名字段相同。我们在内部提交了一个错误。 为了解决这个问题,您可以尝试使用以下代码替换ExternalLoginCallback方法: [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback(string returnUrl) { var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie); if (result == null || result.Identity == null) { return RedirectToAction("Login"); } var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier); if (idClaim == null) { return RedirectToAction("Login"); } var login = new UserLoginInfo(idClaim.Issuer,idClaim.Value); var name = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ",""); // Sign in the user with this external login provider if the user already has a login var user = await UserManager.FindAsync(login); if (user != null) { await SignInAsync(user,isPersistent: false); return RedirectToLocal(returnUrl); } else { // If the user does not have an account,then prompt the user to create an account ViewBag.ReturnUrl = returnUrl; ViewBag.LoginProvider = login.LoginProvider; return View("ExternalLoginConfirmation",new ExternalLoginConfirmationViewModel { UserName = name }); } } 当没有用户名从Facebook /谷歌返回时,代码将默认用户名设置为空。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 带有并发检查的ASP.NET MVC实体框架
- asp.net-mvc – Visual Studio不允许在MVC视图中使用断点
- iis-7 – ASPError对象在我的自定义错误页面上不包含任何数
- asp.net – 发送多个模型以查看MVC 4
- Asp.net下利用Jquery Ajax实现用户注册检测(验证用户名是否
- asp.net-mvc – MVC Razor HTML助手语法:Viewbag in Html.
- 你如何在ASP.NET中配置httpOnlyCookies?
- asp.net-mvc – 渲染位于远程服务器上的部分视图
- asp.net-mvc – ASP.NET MVC 3从同一表单保存和编辑
- 下载 – 使用ASP.Net Webapi流式传输大图像
推荐文章
站长推荐
- asp.net – 如何实现跨数据库外键约束?
- asp.net – App_Start文件夹在ASP 4.5仅在WebApp
- asp.net-core – 在ASP.NET MVC Core中仍然存在V
- asp.net-mvc – 用于Kendo网格模板中的循环
- iis-7 – IIS7和经典ASP会话
- asp.net-mvc – Spark View Engine的性能与ASP.N
- asp.net-mvc – 如果在@ Html.DisplayFor中有条件
- 如何利用ASP.net IIS 7.5中的浏览器缓存
- asp.net – 获取特定的会员提供商
- asp.net-mvc-3 – 使用Razor在Telerik MVC3网格中
热点阅读