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

时间:2019-04-03 19:58:22

标签: c# asp.net-core nlog

该网站将使用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 

为什么会这样?以及如何将所有内容写入“用户”特定文件(在他们登录后)?

1 个答案:

答案 0 :(得分:1)

根据您在此处发布的内容,发生了重定向;返回RedirectToAction。重定向会删除所有cookie信息,因此此时不认识该用户。

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

相关问题