【问题标题】:Authenticate .net web application as specific user将 .net Web 应用程序作为特定用户进行身份验证
【发布时间】:2016-06-20 17:15:18
【问题描述】:

我有一个在域上运行的 .net mvc Web 应用程序,它使用 Windows 身份验证。

我正在研究 react redux 和 webpack-server。可以运行 2 台服务器。一个是 .net web 应用程序,另一个是 webpack-server,它将为 java 脚本提供热加载和其他好处。

要让 webpack-server 从 .net 应用程序提供内容,我可以添加以下内容:

proxy: {
  '*': {
    target: 'http://other-server.example.com'
  }
}

任何对缺失资源的请求都将转到 .net 应用程序。但是这不起作用,因为身份验证不起作用。

是否有可能有一个构建配置文件来执行 web.config 转换,将任何请求设置为以特定用户身份进行身份验证?

或者有谁知道节点服务器如何代理支持windows身份验证的请求?

【问题讨论】:

    标签: .net asp.net-mvc authentication iis-8 webpack-dev-server


    【解决方案1】:

    我找到的解决方案如下:

    在 global.asax.cs 中

        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                if (System.Configuration.ConfigurationManager.AppSettings["anonymous"] == "true")
                {
                    HttpContext.Current.User = new GenericPrincipal(new GenericIdentity("DOMAIN\\Harry-Potter"), new string[] { });
                }
            }
        }
    

    使用 Build => 配置管理器创建了一个构建配置文件.. =>(在 Active solution configuration 下是一个下拉菜单,您可以在其中添加新的)

    右键单击 web.config 并选择“添加配置转换”

    在 web.[您的个人资料].config 中:

    <appSettings>
      <add key="anonymous" value="true" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    

    使用 web.debug.config 和 web.[my build profile].config 对 web.config 的转换在您出于某种原因发布之前不适用。要在每次构建时都可以使用转换,您可以执行以下操作。

    将 web.config 复制到 web.base(web.base 不存在,所以只需 copy web.config web.base

    打开您的 Web 应用程序 csproj 文件(与 web.config 和 web.base 相同的目录)在末尾添加/编辑以下 2 行。

      <PropertyGroup>
        <PostBuildEvent>"$(MSBUILDBINPATH)\msbuild" "$(ProjectPath)" /t:Transform /p:Configuration=$(ConfigurationName);Platform=AnyCPU</PostBuildEvent>
      </PropertyGroup>
      <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
           Other similar extension points exist, see Microsoft.Common.targets.
      <Target Name="BeforeBuild">
      </Target>
      <Target Name="AfterBuild">
      </Target> -->
      <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
      <Target Name="Transform">
        <MakeDir Directories="bin" Condition="!Exists('bin')" />
        <TransformXml Source="Web.base" Transform="Web.$(Configuration).config" Destination="Web.config" StackTrace="true" />
      </Target>
    

    在 web.base 中:

    <appSettings>
      <add key="anonymous" value="false" />
    

    每次构建时都会重新编写 web.config。

    现在,当您切换构建配置文件时,您可以拥有应用程序的真实用户或模拟用户身份(也可以方便地测试其他用户配置文件)。

    在 C:\Users[我的用户]\Documents\IISExpress\config\applicationhost.config 中

    我更改了一些设置以允许匿名用户,如果不是 IIS express 将不允许匿名用户。

    <anonymousAuthentication enabled="true" userName="" />
    

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 2011-06-19
      • 2010-09-26
      相关资源
      最近更新 更多