繁体   English   中英

ASP.NET MVC 5 方法 Session_Start 在 IIS 中无法正常工作

[英]ASP.NET MVC 5 method Session_Start does not work properly in IIS

我需要将 asp.net mvc 5 应用程序发布到 iis 服务器。 目标框架是 4.7.2。 在 Web.Config 文件中定义了 Windows 身份验证

这是 Web.config 文件

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
      <authentication mode="Windows" />
      <authorization>
          <deny users="?" />
      </authorization>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <connectionStrings>
    <!--<add name="DWH_FATTEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VENUS01;initial catalog=DWH_FATT;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />-->
      <add name="DWH_FATT" connectionString="Data Source=VENUS01;initial catalog=DWH_FATT;integrated security=true" />
  <add name="DWH_FATTEntities" connectionString="metadata=res://*/Models.Model_DWH.csdl|res://*/Models.Model_DWH.ssdl|res://*/Models.Model_DWH.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VENUS01;initial catalog=DWH_FATT;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

在 iis 服务器中,我禁用了匿名身份验证并启用了 Windows 身份验证。

在此处输入图像描述

当我尝试从浏览器启动应用程序时,我收到一个错误,例如用户 'DOMAIN \ MACHINENAME $' 的登录失败

这是我从浏览器收到的堆栈跟踪

在此处输入图像描述

奇怪的是,应用程序尝试从本地路径执行 Global.asax 文件中的 Session_Start 方法。 在红色下划线的线中可以看到。

事实上,如果我尝试从 Global.asax 文件中删除 Session_Start 方法,我不会出现此类错误,但此方法中包含的说明对于应用程序的正确运行非常重要。

这是我使用的 Global.asax 文件

public class MvcApplication : System.Web.HttpApplication
    {

        DWH_FATTEntities db = new DWH_FATTEntities();
        utenti utente = new utenti();

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Session_Start(Object sender, EventArgs e)
        {

            string username = User.Identity.Name.Substring(8);
            utente = db.utenti.Where(i => i.alias == username).FirstOrDefault();

            //Context.Items["listmot"] = new MotMainViewModel();

            if (utente != null && utente.utente_dashboard)
            {
                HttpContext.Current.Session.Add("accesso_utente", utente.utente_dashboard);
                HttpContext.Current.Session.Add("username", username);
                HttpContext.Current.Session.Add("nome_utente", utente.nome);
                HttpContext.Current.Session.Add("cognome_utente", utente.cognome);
                HttpContext.Current.Session.Add("mail_utente", utente.mail);
                HttpContext.Current.Session.Add("gruppo_utente", utente.gruppo);
                HttpContext.Current.Session.Add("responsabile_utente", utente.responsabile);

                GetMot();
            }

        }

        public static void GetMot()
        {
            List<MotMainViewModel> listmot = new List<MotMainViewModel>();

            using (var db = new DWH_FATTEntities())
            {
                listmot = (from mot in db.servizi_new
                           join clienti in db.clienti on mot.id_cliente equals clienti.id_cliente into ps
                           from clienti in ps.DefaultIfEmpty()
                           select new MotMainViewModel
                           {
                               n_ordine = mot.n_ordine,
                               data_ordine = mot.data_ordine,
                               ragione_sociale_cliente = clienti.ragione_sociale,
                               data_consegna = mot.data_consegna,
                               data_cessazione = mot.data_cessazione,
                               note = mot.note,
                               rfc = mot.rfc,
                               ros = mot.ros,
                               id_sito_a = mot.id_sito_a,
                               id_sito_b = mot.id_sito_b,
                               qty = mot.qty,
                               tipo_circuito = mot.tipo_circuito,
                               id_cliente = mot.id_cliente,
                               mercato = mot.mercato,
                               id_gca = mot.id_gca,
                               tipo_contratto = mot.tipo_contratto,
                               data_inserimento = mot.data_inserimento,
                               data_invio_cessazione = mot.data_invio_cessazione,
                               motivo_cessazione = mot.motivo_cessazione
                           }).ToList();
            }

            HttpContext.Current.Session.Add("listmot", listmot);
        }
    }

我究竟做错了什么?

提前致谢

您可以尝试以下设置:

  • 在 ApplicationPoolIdentity 下运行的 Web 应用程序
  • Web 应用程序通过 ADO.Net 在连接字符串中使用 Windows 身份验证连接到数据库

如果您的数据库连接处于 Windows 身份验证模式。 您需要将应用程序池标识从 ApplicationPoolIdentity 更改为您的域登录凭据 DomainName\YourloginId

步:

  1. 单击应用程序池
  2. 选择您的应用程序的名称
  3. 进入高级设置
  4. 展开流程模型并单击身份。 单击右端的三个点。
  5. 单击设置...按钮并提供您的域登录凭据

笔记:

在生产或 IT 环境中,您可能在同一域下拥有应用程序池标识的服务帐户。 如果是这样,请使用服务帐户而不是您的登录名。

暂无
暂无

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

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