简体   繁体   English

实现 NTLM 授权的 C# 助手类

[英]C# helper classes to implement NTLM authorization

Currently I'm trying to solve my problem — which is implement NTLM authorization on my intranet site in the way how I think it should work, namely ask password only on certain pages.目前我正在尝试解决我的问题——这是在我的 Intranet 站点上以我认为它应该如何工作的方式实现 NTLM 授权,即仅在某些页面上询问密码。 Not just hitting main page — so site should be divided on two pieces: available for all and restricted.不只是点击主页 - 所以网站应该分为两部分:对所有人可用和受限制。

The issue I'm using Nancy framework and it does not implement NTLM natively.我使用的是 Nancy 框架的问题,它本身没有实现 NTLM。 But this will not stop the real cowboy programmer.但这并不能阻止真正的牛仔程序员。 So I'm trying to develop custom request / response sequence to accomplish this goal.所以我正在尝试开发自定义请求/响应序列来实现这个目标。

For now I have discovered this Q&A , but solution there is glued to the IIS...现在我已经发现了这个 Q&A ,但是那里的解决方案粘在 IIS ...

I have discovered site with a lots of complex information about NTLM and I wondering is there any C# class to simplify this process?我发现了包含大量有关 NTLM 的复杂信息的站点,我想知道是否有任何 C# 类可以简化此过程?

Namely, helping to create responses of different types.即,帮助创建不同类型的响应。

Currently my code looks like this:目前我的代码如下所示:

Get["/Profile/"] = parameters =>
{
    var request = this.Request;

    if (this.Request.Headers.Keys.Any(x => x == "Authorization"))
    {
        var items = Response.Context.Items;

        var expert = new Expert(WindowsIdentity.GetCurrent());
        var model = expert.Ensure();

        return View["Profile.liquid", model];
    }
    else
    {
        var response = new Response();
        response.StatusCode = HttpStatusCode.Unauthorized;
        response.Headers.Add("WWW-Authenticate", "NTLM");
        return response;
    }
};

But it implements only first stage of NTLM authorization.但它只实现了 NTLM 授权的第一阶段。 Is it possible to avoid lots of manual code to implement other steps by involving ready to use helper?是否可以通过使用即用型助手来避免大量手动代码来实现其他步骤?

If you really want to write all this yourself I think you're in for a bit of a mammoth task.如果您真的想自己编写所有这些,我认为您将面临一项艰巨的任务。 This URL may help you, it has information on NTLM auth in general, but also shows an example of the conversation for HTTP authentication using NTLM:这个 URL 可能对你有帮助,它有关于 NTLM 身份验证的一般信息,但也显示了使用 NTLM 进行 HTTP 身份验证的对话示例:

http://davenport.sourceforge.net/ntlm.html#ntlmHttpAuthentication http://davenport.sourceforge.net/ntlm.html#ntlmHttpAuthentication

Another possible avenue to explore is to see if there's anything in the Mono code base that you can make use of - that's what we did with the built in JSON serializer.另一个可能的探索途径是查看 Mono 代码库中是否有任何您可以使用的内容 - 这就是我们对内置 JSON 序列化程序所做的。

Another option is to use forms or basic auth, but authenticate the usernames/passwords against AD/LDAP.另一种选择是使用表单或基本身份验证,但根据 AD/LDAP 对用户名/密码进行身份验证。

I have developed, merging several sources, a working implementation of the whole protocol: "NTLM"->"NTLM with client data"->"NTLM challenge"->"NTLM challenge from client" and everything works well and without the need for external liberaries.我已经开发,合并了几个来源,整个协议的工作实现:“NTLM”->“带有客户端数据的 NTLM”->“NTLM 挑战”->“来自客户端的 NTLM 挑战”,一切运行良好,不需要外部库。 Only little problem is all C++ (hate playing with buffers in C# :P ), it's a 140kb C++ source.唯一的小问题是所有 C++(讨厌在 C# 中使用缓冲区:P),它是一个 140kb 的 C++ 源代码。 Everything can be found here: https://kendar.org/?p=/dotnet/kendarntlmlib一切都可以在这里找到: https : //kendar.org/?p=/dotnet/kendarntlmlib

--HERE START BAD NEWS...-- --这里开始坏消息...--

as far as i understood on IIS this kind of things can work only as an ISAPI filter.据我在 IIS 上的理解,这种事情只能作为 ISAPI 过滤器工作。 Being NTLM a -connection based- protocol I were not able to do the request-response-request in the same http request while in an MVC controller, aspx page or ashx handler.作为 NTLM 一个基于连接的协议,我无法在 MVC 控制器、aspx 页面或 ashx 处理程序中时在同一个 http 请求中执行请求-响应-请求。 And IIS does not expose any socket handle that can be used to "override" the standard connection-less approach of http but in the ISAPI part... (it's HTTP after all, but it's cutting my wings :P )而且 IIS 没有公开任何可用于“覆盖”http 的标准无连接方法的套接字句柄,但在 ISAPI 部分......(毕竟它是 HTTP,但它正在削减我的翅膀:P)

I hoped to use it like the basic authentication attribute i've seen used on Bonobo github clone... but no luck..我希望像我在 Bonobo github 克隆上看到的基本身份验证属性一样使用它......但没有运气......

--HERE FINISH BAD NEWS-- --这里结束坏消息--

Plus i had some problem loading a native DLL into a C#-ANyCPU compiled project, but this is easy :P ( http://blogs.msdn.com/b/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx just for reference)另外,我在将本机 DLL 加载到 C#-ANyCPU 编译项目中时遇到了一些问题,但这很容易 :P ( http://blogs.msdn.com/b/jorman/archive/2007/08/31/loading-c- assembly-in-asp-net.aspx仅供参考)

I need an example of using https://github.com/toolchain/Nancy.Authentication.Ntlm with users restrictions on some Nancy views.我需要一个使用https://github.com/toolchain/Nancy.Authentication.Ntlm并限制用户对某些 Nancy 视图的示例。 Thank you!谢谢!

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

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