通过ASP.NET MVC填充IIS用户名?

问题描述 投票:1回答:1

对于我们的系统,我们有各种Web API调用,这些调用是通过.NET MVC在内部处理的。这些都是通过使用AuthorizeAttribute的令牌头授权的通用Web调用,这意味着我们的IIS日志显示了一个匿名用户。如果可能的话,我们希望填充该用户名。我认为使用IAuthenticationFilter和更新Principal就可以做到这一点,但是生成的IIS日志的用户名是空白的(匿名)。示例代码如下。

public class UserAuthenticateAttribute : Attribute, IAuthenticationFilter
{
    public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
        //Uses data from within context.ActionContext to populate user data
        context.Principal = new GenericPrincipal(new GenericIdentity("Test User"), null);
    }

    public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
    {
        return Task.FromResult(0);
    }
    public virtual bool AllowMultiple { get { return false; } }
}

我猜测IIS日志是在发送请求的时候生成的 在被AuthenticateAsync接收到之前 有没有一种方法可以做到被IIS日志接收到?

c# asp.net asp.net-mvc iis iis-7
1个回答
1
投票

IIS将记录应用池身份而不是网站身份。enter image description here 如果你在IIS认证模式中禁用了 "匿名认证",你的用户将不得不对网站进行认证。假设我们已经启用了windows认证,IIS将记录网络凭证。enter image description here

字段:日期 时间 s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus

sc-win32-status timetaken 2020-06-04 07:27:48fe80::a4fe:6d79:f2b8:d031%6 GET - 8002 VABQIA969VM\Administratorfe80::a4fe:6d79:f2b8:d031%6 Mozilla5.0+(Windows+NT+10.0.0)。

不过,网站身份是另外一回事,不会记录在IIS日志中,详情请看这个链接。https:/serverfault.comquestions40328in-iis-7-0-what-is-thedifference-between-application-pool-identity-and。 为了记录网站的身份,我建议你使用Asp.Net Core内置的日志机制。https:/www.tutorialsteacher.comcoreaspnet-core-logging https:/nblumhardt.com201610aspnet-core-file-logger。 https:/docs.microsoft.comen-usaspnetcorefundamentalslogging?view=aspnetcore-3.1。 如果有什么我可以帮助的,请随时告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.