繁体   English   中英

文件名中的aspnet-user-identity,但它并不总是有效

[英]aspnet-user-identity in filename, but it doesn't always work

该网站将使用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信息,因此此时不认识该用户。

您总是会遇到用户上下文未知的情况。 就像用户进入网站时一样,但要先登录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM