c# - httphandler 无法正常工作

标签 c# asp.net httphandler

只有当用户登录到不同的页面时,我才尝试重定向用户。我正在使用 HTTPHandler 拦截此请求并重定向。用户登录后,控件不会返回此 HTTPHandler。任何想法或建议

namespace NES.HiLo.Security
{
    public class PallativeAuthenticationHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>


        public bool IsReusable
        {
        // Return false in case your Managed Handler cannot be reused for another request.
        // Usually this would be false in case you have some state information preserved per request.
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {

        string UserName = "";
        int TSecUserID = 0;

        HttpContext context2 = HttpContext.Current;

        if (string.IsNullOrEmpty(context2.User.Identity.Name))
            UserName = "UNKNOWN";
        else
            UserName = context2.User.Identity.Name.ToString();

        if (UserName != "UNKNOWN")
        {

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NES.HiLo.Data.Properties.Settings.HiLoConnectionString"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {

                connection.Open();
                SqlCommand command = new SqlCommand("Select tSec_UserId from dbo.HiLoUser where Username='" + HttpContext.Current.User.Identity.Name.ToString() + "'", connection);


                TSecUserID = (Int32)command.ExecuteScalar();
                connection.Close();
                HttpContext.Current.Response.Redirect("http://www.google.com?retUrl=" + TSecUserID);

            }

        }
        else
        {
            HttpContext.Current.Response.Redirect("~/Login.aspx?retUrl=" + HttpUtility.UrlEncode(context2.Request.Url.ToString()));

        }



    }
}

web.config 中的 Httphanlder 条目

<httpHandlers>
        <!--<add path="*.pdf" type="HttpSecurity.HttpHandlerAuthentication, HttpSecurity"  verb="*"/>-->

        <!--<add verb="GET" path="/calderdale/*/*.pdf" type="NES.HiLo.Security.CalderDaleAuthenticationHandler, NES.HiLo.Security" />-->
        <add verb="*" path="/calderdale/*.pdf" type="NES.HiLo.Security.CalderDaleAuthenticationHandler, NES.HiLo.Security" />
        <remove verb="*" path="*.asmx" />
        <!-- ASPNETAJAX -->
        <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add verb="*" path="*_AppService.axd" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
        <!-- UMBRACO CHANNELS -->
        <add verb="*" path="umbraco/channels.aspx" type="umbraco.presentation.channels.api, umbraco" />
        <add verb="*" path="umbraco/channels/word.aspx" type="umbraco.presentation.channels.wordApi, umbraco" />
        <add verb="*" path="umbraco/clt/ajaxCommunityAdministrators.aspx" type="NES.HiLo.UserControls.DataTypes.AjaxCommunityAdministrators, NES.HiLo" />
        <!-- ELMAH -->
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
        <!-- WIDGET AJAX HANDLER -->
        <add verb="*" path="umbraco/WidgetLibrary/WidgetAjaxHandler.aspx" type="NES.WidgetLibrary.WidgetAjaxHandler" />
        <add verb="GET" path="umbraco/WidgetLibrary/SubjectSelectorAjaxHandler.aspx" type="NES.WidgetLibrary.MetaDataControls.ChildControls.SubjectSelectorControl.AjaxSelector" />
        <add verb="*" path="/FilterByDevice.ashx" type="NES.HiLo.Web.Handlers.DeviceFilterHandler" />
        <add verb="GET" path="/Pallative/*.xml" type="NES.HiLo.Security.PallativeAuthenticationHandler, NES.HiLo.Security" />
    </httpHandlers>




 <authentication mode="Forms">
        <forms name="KFCSAUTH" loginUrl="login.aspx" protection="All" slidingExpiration="true" path="/" domain=".scot.nhs.uk" />
    </authentication>
    <authorization>
        <allow users="?" />
    </authorization>

 <system.webServer>
    <!--<validation validateIntegratedModeConfiguration="false" />-->
    <handlers>
        <add name="Pallative Handler" path="/Pallative/*.xml" verb="GET" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
        <add name="Calderdale Handler" path="/calderdale/*.pdf" verb="GET" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
        <!--<add name="Pallative Handler" path="Pallative/pallative_doc.html" verb="GET" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" />-->
    </handlers>
    <!--<handlers  accessPolicy="Read, Write, Script, Execute">
      -->
    <!--<add name="PictHandler" preCondition="integratedMode" verb="*" path="*.pictx" type="PictHttpHandler,PictHandler"/>-->
    <!--
      <add name="Pdfhandler" verb="*" path="/calderdale/*.html" type="NES.HiLo.Security.CalderDaleAuthenticationHandler, NES.HiLo.Security" preCondition="integratedMode" />
  </handlers>-->

</system.webServer>

最佳答案

您遇到的问题与身份验证 cookie 仅对指定域有效有关:

<forms name="KFCSAUTH" 
    loginUrl="login.aspx" 
    protection="All" 
    slidingExpiration="true" 
    path="/" 
    domain=".scot.nhs.uk" />

也许只有当您在其他某个域中运行 Web 应用程序时才会出现此问题。尝试删除 domain 属性并查看问题是否仍然存在。

关于c# - httphandler 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14667803/

相关文章:

c# - 如何禁用 RavenDB 复制

c# - 如何显示具有焦点外观但不焦点的表单?

c# - Visual Studio 错误列表窗口在生成时打开

c# - ASP.NET Core 中的 HttpRuntime.AppDomainAppPath 等价物

c# - 更新服务引用时出现错误 HRESULT E_FAIL 已从对 COM 组件的调用返回

c# - 在 ASP.NET 中使用 JQuery UI 自动完成的有效方法

java - android.view.View.requestLayout(View.java :16431)

c# - 如何在 IIS 下使用 HTTP 处理程序处理长时间运行的请求?

c# - 如何创建自定义 httpHandler 并将其添加到现有 IIS 网站

c# - Visual Studio 设计时属性 - 表单列表下拉列表