南希:如果可以的话,使用NTLM,如果没有,请匿名进行

时间:2016-10-11 18:42:58

标签: .net webserver windows-authentication ntlm nancy

我有一台Nancy服务器,它成功发布了NTLM挑战并解析了用户。

internal class Program
{
    private static void Main(string[] args)
    {
        using (WebApp.Start<Startup>("http://localhost:9000"))
        {
            Console.WriteLine("Press Enter to quit.");
            Console.ReadKey();
        }
    }
}

internal class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var listener = (HttpListener)app.Properties["System.Net.HttpListener"];
        listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

        app.UseNancy();
    }
}

然而,我真正喜欢的是NTLM如果可用的话。即使NTLM身份验证步骤失败,我仍希望继续处理,以便以类似于以下方式支持处理:

    public StatusModule()
    {
        Get["/"] = _ =>
        {
            var requestEnvironment = (IDictionary<string, object>) Context.Items["OWIN_REQUEST_ENVIRONMENT"];
            var user = (IPrincipal) requestEnvironment["server.User"];

            var userDetails = user == null
                ? null
                : new
                {
                    Username = user.Identity.Name,
                    IsAuthenticated = user.Identity.IsAuthenticated,
                    AuthenticationType = user.Identity.AuthenticationType
                };

            return Response.AsJson(new {TimeStamp = DateTime.Now.ToString("O"), Status = "OK", UserDetails = userDetails});
        };
    }

如何允许用户使NTLM失败但仍处理请求?

0 个答案:

没有答案