- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个网络项目,其中包含使用 cookie 的授权系统。我正在使用 Identity 2.0 示例项目,所以我得到了内置的功能。我还使用 Git 作为我的版本控制系统。
我合并了两个分支。他们都可以轻松地使用授权和 cookie。当您登录然后通过 Visual Studio 停止应用程序时,我可以再次启动该应用程序并且我将保持登录状态。
在我合并了两个分支之后我得到了一些奇怪的行为。我合并的代码不会影响授权系统。
当您登录时会创建 cookie 文件,因此当我通过 Visual Studio 手动停止应用程序然后再次启动它时,我应该已登录。但是应用程序崩溃并出现以下异常:
An exception of type 'System.NullReferenceException' occurred in Microsoft.Owin.Host.SystemWeb.dll but was not handled in user code
Visual Studio 指向以下行:
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
该代码包含在 Identity Sample 中,并且在合并之前一直运行良好。正如我所发现的,如果我删除以下代码,该应用程序可以正常运行;我可以停止它并再次运行它,零异常:
@{
var manager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = manager.FindById(User.Identity.GetUserId());
}
@Html.ActionLink("Hello " + currentUser.UserNickName + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
此代码是 _LoggingPartial View 的一部分,它由 Visual Studio 生成并在我合并的分支中修改。
所以现在我完全迷路了,因为我不知道那个异常是由什么引起的。
也许有人可以帮助我,就如何解决它给出建议。
据我了解,问题不在于昵称。实际上,即使我将以下代码添加到旧版本中,应用程序也会在停止启动后崩溃:
@{
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = userManager.FindById(User.Identity.GetUserId());
}
@Html.ActionLink("Hello " + currentUser.Email.ToString() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
如您所见,此代码不使用任何方法,而是使用 native Identity 的方法。
另外,我测试了不同的方法来获取用户并得到了以下结果:
如果我使用“var currentUser = userManager.FindById(User.Identity.GetUserId());”,应用崩溃
如果我使用“var currentUser = userManager.FindByIdAsync(User.Identity.GetUserId());”,应用崩溃 (当然,我已经修改了其余代码以处理异步结果)
如果我使用“var currentUser = userManager.FindByName(User.Identity.GetUserName());”,应用崩溃
如果我使用“var currentUser = userManager.FindByNameAsync(User.Identity.GetUserName());”,应用不会崩溃
因此,它既不适用于 FindById,也不适用于 FindByIdAsync。如果我使用 FindByName,它可以与 FindByNameAsync 一起使用,但会因简单的 FindByName 而崩溃
最佳答案
找出错误的原因。当我们第一次登录时,我们的用户信息保存在 cookie 中。由于我们使用字符串 Id(在 Identity 中默认),userId = someRandomHash()。也许不是随机的,但无论如何它都不是恒定的。所以,我们的 cookie 中有 randomString1。当我们停止服务器并再次启动它时,数据库将再次生成,因为我们将数据库初始化定义为“DropCreateAlways”。因此,我们的数据库中有 randomString2,但 cookie 中有 randomString1。当我们尝试从 cookie 中获取保存的 Id 时,我们成功获取了它,但是我们无法通过 userManager 在数据库中找到它,因为 Id 现在不同了。
此外,它还解释了为什么使用 GetName() 方法无法重现错误 - 我们始终拥有相同的用户名。而且它只适用于 async Find() 因为它在我们仍然有数据库时运行。然而,简单的非异步方法在数据库已经被删除并且我们在那里没有任何名称时运行。仍然不知道为什么 Visual Studio 会指向“var userManager”行,但我猜这不是很重要。
关于c# - 在给定用户已经有 cookie 的情况下启动应用程序时处理 null HttpContext.Current,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26694655/
在我的主要组件中,我有: mounted() { window.$cookie.set('cookie_name', userName, expiringTime); }, 这会产生以下错误:
我正在学习 cookie,并且我想知道在编写依赖 cookie 来存储状态的 Web 应用程序时浏览器的支持情况。 对于每个域/网站,可以向浏览器发送多少个 Cookie,大小是多少? 如果发送并存储
我已经为我的站点设置了一个 cdn,并将其用于 css、js 和图像。 网站只提供那些文件 我的问题是 firefox 中的页面速度插件对于我的图片请求,我看到了一个 cookie Cookie fc
在阅读了 Internet 上的文档和帖子后,我仍然无法解决 jMeter 中的 Cookie Manager 问题。 我在响应头中得到了 sid ID,但它没有存储在我的 cookie 管理器中。
我正在 Node.JS 中处理一些类似浏览器的 cookie 处理,想知道从 NodeJS and HTTP Client - Are cookies supported? 开始对这段代码进行扩展到什
我正在此堆栈上构建自托管 Web 服务器:欧文南希网络 API 2 我正在使用 Katana 的 Microsoft.Owin.Security.Cookies 进行类似表单的身份验证。我得到了 Se
我有一个从另一个网站加载资源的网站。我已经能够确定: 第三方网站在用户的浏览器上放置 cookie。 如果我在浏览器设置中禁用第三方 cookie,第三方网站将无法再在浏览器上放置 cookie。 该
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我正在使用 python mechanize 制作登录脚本。我已经读到 Mechanize 的 Browser() 对象将自动处理 cookie 以供进一步请求。 我怎样才能使这个 cookie 持久
我正在尝试在 www.example.com 和 admin.other.example.com 之间共享 cookie 我已经能够使其与 other.example.com 一起使用,但是无法访问子
我设置了一个域为 .example.com 的 cookie .它适用于我网站上的每个一级子域,应该如此。 但是,它不适用于 n 级子域,即 sub.subdomain.example.com和 to
我想让用户尽可能长时间地登录。 我应该使用什么? 普通 cookies 持久性 cookie 快闪 cookies ip地址 session 或这些的某种组合? 最佳答案 我认为 Flash cook
如果给定的 Web 服务器只能读取其域内设置的 cookie,那么 Internet 广告商如何从其网络外的网站跟踪用户的 Web 流量? 是否存在某种“supercookie”全局广告系统,允许广告
我知道一个 cookie 可以容纳多少数据是有限制的,但是我们可以设置多少个 cookie 有限制吗? 最佳答案 来自 http://www.ietf.org/rfc/rfc2109.txt Prac
如果我拒绝创建 cookie,则在我的浏览器中创建名称为 __utma、__utmb 等的 cookie。我认为这个 cookie 是用于谷歌分析的。任何人都知道谷歌如何创建这个 cookie,即使浏
我有一个生产环境和一个登台环境。我想知道我是否可以在环境之间沙箱 cookie。我的设置看起来像 生产 domain.com - 前端 SPA api.domain.com - 后端节点 分期 sta
我想知道浏览器(即 Firefox )和网站的交互。 当我将用户名和密码提交到登录表单时,会发生什么? 我认为该网站向我发送了一些 cookie,并通过检查这些 cookie 来授权我。 cookie
我在两个不同的域中有两个网络应用程序 WebApp1 和 WebApp2。 我在 HttpResponse 的 WebApp1 中设置 cookie。 如何从 WebApp2 中的 HttpReque
我正在使用Dartium“Version 34.0.1847.0 aura(264987)”,并从Dart创建一个websocket。但是,如果不是httpOnly,我的安全 session cook
我从 Headfirst Javascript 书中获取了用于 cookie 的代码。但由于某种原因,它不适用于我的浏览器。我主要使用chrome和ff,并且我在chrome中启用了本地cookie。
我是一名优秀的程序员,十分优秀!