以特定用户身份验证.net Web应用程序

时间:2016-06-20 17:15:18

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

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

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

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

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

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

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

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

1 个答案:

答案 0 :(得分:0)

我发现的溶剂如下:

在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并选择"添加配置转换"

在网络中。[您的个人资料] .config:

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

使用web.debug.config和web转换到web.config。[我的构建配置文件] .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快递不会让匿名用户。

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