【问题标题】:Windows authentication in asp.net 5asp.net 5 中的 Windows 身份验证
【发布时间】:2015-02-16 13:08:43
【问题描述】:

我正在用 ASP .NET 5、MVC 6 构建一个 Intranet 应用程序。我想知道如何启用 Windows 身份验证。?默认项目模板仅支持个人用户帐户。

【问题讨论】:

  • 如果您打算在 IIS 上托管您的网站,您可以在 IIS 中配置 windows 身份验证
  • 自助托管等其他选项呢?
  • 写个中间件我猜
  • @user2935752 我知道已经有一段时间了,但如果您还在寻找,请参阅我的回答,了解如何为自托管项目启用此功能。

标签: c# windows-authentication asp.net-core asp.net-core-mvc


【解决方案1】:

Mark 的回答在 ASP.Net RC1 中仍然有效。有一些额外的步骤可以将它们联系在一起(我没有足够的声誉来评论他的解决方案):

  1. 安装WebListener from NuGet
  2. 在 Startcup.cs 中添加以下用法:

    using Microsoft.AspNet.Http.Features;
    using Microsoft.Net.Http.Server;
    
  3. 在app.UseMvc之前的Configure方法中添加Mark's code snippet

    // If we're self-hosting, enable integrated authentication (if we're using
    // IIS, this will be done at the IIS configuration level).
    var listener = app.ServerFeatures.Get<WebListener>();
    if (listener != null)
    {
        listener.AuthenticationManager.AuthenticationSchemes = 
        AuthenticationSchemes.NTLM;
    }
    
  4. 要调试此问题,您需要在 project.json 中输入 add the WebListener run target,正如 Mark 在另一个答案中指出的那样:

    "commands": {
      "weblistener": "Microsoft.AspNet.Server.WebListener --config hosting.ini",
      "web": "Microsoft.AspNet.Server.Kestrel"
    },
    
  5. 选择 weblistener 而不是 IIS Express of web (Kestrel) 来调试您的应用程序。

【讨论】:

    【解决方案2】:

    除了此处仅适用于 IIS 托管的其他答案之外,您还可以通过在 Startup 中添加以下内容,在自托管 ASP.NET 5 项目(针对 beta 7 和 beta 8 测试)中启用 Windows 身份验证.cs Configure 方法,在您希望保护的 app.UseMvc 或类似之前:

    测试版 8 更新

    // If we're self-hosting, enable integrated authentication (if we're using
    // IIS, this will be done at the IIS configuration level).
    var listener = app.ServerFeatures.Get<WebListener>();
    if (listener != null)
    {
        listener.AuthenticationManager.AuthenticationSchemes = 
            AuthenticationSchemes.NTLM;
    }
    

    Beta 7 的先前答案

    // If we're self-hosting, enable windows/integrated authentication.
    // For IIS, this needs to be configured in IIS instead, and the
    // following will have no effect.
    if ((app.Server as ServerInformation) != null)
    {
      var serverInformation = (ServerInformation)app.Server;
      serverInformation.Listener.AuthenticationManager.AuthenticationSchemes = 
          AuthenticationSchemes.NTLM;
    }
    

    改编自官方MusicStore example

    如果您正在使用带有 IIS Express 的 Visual Studio 2015 进行调试,您现在可以通过项目调试属性页面中的复选框打开 Windows 身份验证,而不是弄乱applicationhost.config 文件。我无法让 web.config 解决方案用于 IIS Express 调试,它会引发有关配置在该级别无效的错误。 请注意,这目前在 beta 8 中不起作用 - 请参阅 this issue

    【讨论】:

      【解决方案3】:

      $(ProjectDir)\Properties\launchSettings.json 文件将触发 Visual Studio 在适当调试 IISExpress 时生成一个web.config 文件,该文件将根据启动设置设置&lt;authentication/&gt; 节点。

      下面是一个例子launchSettings.json

      {
        "iisSettings": {
          "windowsAuthentication": true,
          "anonymousAuthentication": false,
          "iisExpress": {
            "applicationUrl": "http://localhost:65070/",
            "sslPort": 0
          }
        },
        "profiles": {
          "IIS Express": {
            "commandName": "IISExpress",
            "launchBrowser": true,
            "environmentVariables": {
              "Hosting:Environment": "Development"
            }
          },
          "web": {
            "commandName": "web",
            "environmentVariables": {
              "Hosting:Environment": "Development"
            }
          }
        }
      }
      

      但也可以使用扩展名app.UseIISPlatformHandler(); 而不是操纵侦听器。该扩展将设置一个中间件,该中间件将自动请求 NTLM 并从 IIS 转换适当的句柄。

      部署到 IIS 时,如果您使用的是 WebListener,您必须自己将 authentication 节点添加到 web.config。如果您使用HttpPlatformHandler(我个人推荐)并代理红隼,请将forwardWindowsAuthToken="true" 添加到web.config 中的httpPlatform 节点。

      【讨论】:

        【解决方案4】:

        使用 IIS 托管,您可以将 web.config 文件添加到您的 wwwroot 目录,其中包含应用程序的 IIS 配置。

        web.config

        <?xml version="1.0" encoding="utf-8"?>
        <configuration>
          <system.webServer>
                <security>
                    <authentication>
                        <windowsAuthentication enabled="true" />
                        <anonymousAuthentication enabled="false" />
                    </authentication>
                </security>
          </system.webServer>
        </configuration>
        

        【讨论】:

          【解决方案5】:

          我做了我在互联网上找到的一切,没有人工作。所以,我查看了 aspnet 4.5 配置文件,我看到它使用:

          <IISExpressAnonymousAuthentication>disabled</IISExpressAnonymousAuthentication>
          <IISExpressWindowsAuthentication>enabled</IISExpressWindowsAuthentication>
          

          在 .csproj 文件上,我刚刚复制到 aspnet 5 的 .xproj 文件,它就可以工作了。

          【讨论】:

            【解决方案6】:

            因为您正在构建一个新的应用程序,您可以通过单击Change Authentication 来更改身份验证类型。这将弹出一个选项,您可以在其中将类型类型更改为 Windows 身份验证。

            【讨论】:

              【解决方案7】:

              对于来自空 Web 应用程序的 RC1 和 IISExpress:

              • 右键web项目,选择Properties
              • 点击Debug标签,查看Enable Windows Authentication

              这对~/Properties/launchSettings.json 的影响如下:

              "windowsAuthentication": true,
              "anonymousAuthentication": false,
              

              【讨论】:

                【解决方案8】:

                您需要手动配置 IIS Express(在 VS2015 CTP6 中)。 为此,请编辑 applicationhost.config 文件。 (C:\Users\你的用户名\Documents\IISExpress\config\applicationhost.config)

                在配置标签中添加:

                <location path="{your site name}">
                    <system.webServer>
                        <security>
                            <authentication>
                                <windowsAuthentication enabled="true" />
                                <anonymousAuthentication enabled="false" />
                            </authentication>
                        </security>
                    </system.webServer>
                </location>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-12-21
                  • 1970-01-01
                  • 2015-08-06
                  • 2021-05-31
                  相关资源
                  最近更新 更多