c# - 文件名中的 aspnet-user-identity,但它并不总是有效
问题描述
该网站将使用 Cookie 身份验证。因此,这是我的 nlog.config 中的一个示例(我可能对使用 NLog 感兴趣):
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\${aspnet-user-identity}-nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\${aspnet-user-identity}-nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>
对于这两个条目,请查看 ${aspnet-user-identity}。
用户登录后,所有内容都将写入 MyUserName-nlog-all-2019-04-03.log。这样我就可以按用户登录。
我所看到的是,即使我已登录,某些内容也不会写入 MyUserName-nlog-all-2019-04-03.log。而是将它们写入 -nlog-all-2019-04-03.log。注意到前面的破折号了吗?没有用户名?我的意思是,大多数确实会写入“MyUser”,但有些东西不会。
我的小测试项目有一个用于登录的帐户控制器/视图,然后是一个简单的控制器/视图,它们在登录后被定向到。
想象一下进入登录页面,登录并被定向到您的第一页。好吧,在那次小测试之后,我看到了这个 -nlog 文件,里面写着这个(这是在他们登录之后):
2019 - 04 - 03 15:53:39.2429 | 2 | INFO | Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker | Executed action method TestApp1.Controllers.AccountController.Login(TestApp1), returned result Microsoft.AspNetCore.Mvc.RedirectToActionResult in 2263.1283ms.
2019 - 04 - 03 15:53:39.2490 | 1 | INFO | Microsoft.AspNetCore.Mvc.RedirectToActionResult | Executing RedirectResult, redirecting to /.
2019 - 04 - 03 15:53:39.2490 | 2 | INFO | Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker | Executed action TestApp1.Controllers.AccountController.Login(TestApp1) in 2306.6386ms
2019 - 04 - 03 15:53:39.2490 | 1 | INFO | Microsoft.AspNetCore.Routing.EndpointMiddleware | Executed endpoint 'TestApp1.Controllers.AccountController.Login (TestApp1)'
2019 - 04 - 03 15:53:39.2490 | 2 | INFO | Microsoft.AspNetCore.Hosting.Internal.WebHost | Request finished in 2328.4375ms 302
2019 - 04 - 03 15:53:39.2490 | 1 | INFO | Microsoft.AspNetCore.Hosting.Internal.WebHost | Request starting HTTP/ 1.1 GET http://localhost:61610/
2019 - 04 - 03 15:53:40.3210 | 1 | INFO | Microsoft.AspNetCore.Hosting.Internal.WebHost | Request starting HTTP/ 1.1 POST http://localhost:61610/HomeController/Read application/x-www-form-urlencoded; charset=UTF-8 20
为什么会这样?以及如何将所有内容写入“用户”特定文件(在他们登录后)?
解决方案
根据您在此处发布的内容,发生了重定向;RedirectToAction
被退回。重定向会丢弃所有 cookie 信息,因此此时用户不知道。
您总是会遇到不知道用户上下文的情况。就像用户访问网站时一样,但在他们登录之前。
推荐阅读
- python - 关于pyqt5 manubar热键
- spring-boot - 领事拒绝连接
- jenkins - 参数可以用作方法体以使其在 Jenkins 管道中可重用吗?
- python - SQLite python - 参数的类型不受支持
- android - react-native-webview 不适用于 android (RN 0.60.5)
- javascript - 按顺序执行promises map
- sql-server - JOOQ:案例表达式在为空时更新列值时抛出错误
- json - 创建 JSON 模式所需的帮助
- gradle - 未找到 ID 为“org.ajoberstar.grgit”的插件
- android - 无法在 Android 活动中显示 Admob 广告