c# - ASP.NET vNext Kestrel + Windows 身份验证

标签 c# asp.net-mvc linux kestrel-http-server

我在 linux 系统上安装了一个 ASP.NET web 应用程序,运行良好,我可以浏览所有内容。现在我想知道,是否有可能或解决方法将 Windows 身份验证方案与 Kestrel 服务器一起使用?正如我在谷歌上搜索后看到的那样,目前不支持此功能。

感谢您的回答。

编辑:

但是 windows 身份验证在 Kestrel 中不起作用,我能够通过在我的 webb 应用程序中实现 NTLM 身份验证的质询-响应协议(protocol)来获取用户名。

基于 https://loune.net/2009/09/ntlm-authentication-in-php-now-with-ntlmv2-hash-checking/

最佳答案

好的,我接受,Windows 身份验证在 Linux 中不可用,但是使用我问题中的链接,我们可以编写一个自定义的 ActionFilterAttribute,这使得 NTLM 身份验证过程:

public async override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var controller = (ControllerBase)filterContext.Controller;
    if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
        if (!(await IsAuthorized(filterContext.HttpContext, controller)))
        {
            filterContext.Result = new HttpStatusCodeResult(401);
        }
    }
    else
    {
        //TODO check access rights
    }
}

private async Task<bool> IsAuthorized(HttpContext context, ControllerBase controller)
{
    if (!context.Request.Headers.ContainsKey("Authorization"))
    {
        context.Response.Headers.Add("WWW-Authenticate", "NTLM");
        return false;
    }

    var header = context.Request.Headers["Authorization"][0].Substring(5);
    var message = System.Text.Encoding.ASCII.GetString(
                      Convert.FromBase64String(header));
    if (!message.StartsWith("NTLMSSP"))
    {
        return false;
    }

    //type 1 message received
    if (message[8] == '\x01')
    {
        byte[] type2Message =
        {
            0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00,//Signature
            0x02, 0x00, 0x00, 0x00, //Type 2 Indicator
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Security Buffer
            0x01, 0x02, 0x81, 0x00, //Flags
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Challenge
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Context
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //Target info
        };
        context.Response.Headers.Add("WWW-Authenticate", "NTLM "
            + Convert.ToBase64String(type2Message));
        return false;
    }
    //type 3 message received
    else if (message[8] == '\x03')
    {
        var userName = GetMessageString(message, 36);
        var domainName = GetMessageString(message, 28);
        var workstation = GetMessageString(message, 44);

        var user = controller.DbContext.Users.FirstOrDefault(
                      u => u.WindowsAccount.ToLower() == userName.ToLower());
        if (user != null)
        {
            var identity = new ClaimsIdentity();
            identity.AddClaim(new Claim(ClaimTypes.Name, userName));
            context.User.AddIdentity(identity);

            try
            {
                await controller.SignInManager.SignInAsync(user, false);
            }
            catch { }

            return true;
        }
    }

    return false;
}

private string GetMessageString(string message, int start, bool unicode = true)
{
    var length = message[start + 1] * 256 + message[start];
    var offset = message[start + 5] * 256 + message[start + 4];
    if (unicode)
        return message.Substring(offset, length).Replace("\0", "");
    else
        return message.Substring(offset, length);
}

关于c# - ASP.NET vNext Kestrel + Windows 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34956808/

相关文章:

c# - 根据另一个列值插入唯一序列号

C# ASP.NET MVC : When to update LastActivityDate?

c++ - 在 Debian 上使用 AX_BOOST_BASE 找不到 boost

c - 在c中使用execvp执行touch

c# - 数据表 Select() 方法

c# - 脱机时未分配 Azure 移动服务标识列

asp.net - web.config 文件中的自定义错误页面未涵盖所有 url 路径

linux - 为所有用户和进程设置路径

c# - 当用户选择一条记录时,推荐使用哪种方式来填充 Web 表单上的所有控件?

c# - 使用 Grid.Mvc 发布所有选定的行