【问题标题】:How to make the signalr client avoid form authentication?如何使信号器客户端避免表单身份验证?
【发布时间】:2014-12-15 22:36:53
【问题描述】:

我的 MVC 网站在 web.config 中有一个表单授权。 现在所有页面都需要通过授权才能查看。

但是现在我有一个控制台程序(C# 控制台程序)。这个程序需要发送一些消息到 SignalR 集线器。 但是我的 signalR Hub 在我的 MVC 网站中,现在这个 C# 控制台客户端无法向 Signal Hub 发送消息, 因为我的 MVC 网站有表单授权。

我希望控制台客户端不需要验证,但网站中的页面需要验证。

我可以用网站的 web.config 文件做什么???

<location path="signalr/send" >
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="Home/pageone" >
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <authorization>
      <deny users="?"/>
      <allow users="*"/>
    </authorization>-->
</system.web>

这样,我只能让pageone不需要验证,但它不适用于signalR客户端程序

有我的 web.config 文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>


  <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>

  <location path="Home/Index3" ><!--this way is working ,I can make the index3 page could be viewed with no authentication-->
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="signalr/send" >  <!--this url is got by Request.Url.AbsoluteUrl  in the hub class,but this way is not working-->
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
   <location path="refreshhub/senditems" >  <!--my hub is named as "RefreshHub" and the method the client revoked is "senditems",  but this configuration is not working either-->
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <authentication mode="Forms">  <!--I give my website all page Form authentication-->
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <authorization>
       <deny users="?"/>
      <allow users="*"/>
    </authorization>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.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.5.2.14234" newVersion="1.5.2.14234" />
      </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="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

【问题讨论】:

    标签: c# asp.net signalr


    【解决方案1】:
    <deny users="?"/>
    <allow users="*"/>
    

    您拒绝匿名用户(仅允许经过身份验证的用户),然后允许所有用户。 一旦匿名用户被拒绝访问,它将看不到允许用户的下一行。允许规则应该放在第一位。

    编辑: 我假设您的 signalR 集线器 refreshhub 位于根目录中。那么表达式将是:

    <location path="refreshhub.vb " > (or  <location path="refreshhub.cs”>)
    

    如果集线器在某个文件夹中,比如 myfolder,则表达式为:

     <location path="myfolder/refreshhub.vb" >
    

    位置路径是页面、图像、文件夹等资源,而不是方法(据我所知)。

    关于在中心使用特定方法的授权,请参阅 http://www.asp.net/signalr/overview/security/hub-authorization

    【讨论】:

    • 但是,如果这样,表单认证对其他页面不起作用
    • 只需尝试在您的 Home/pageone 的位置路径规则之后添加 Signal R Hub 的位置路径规则,并使用允许用户 =“*”。
    • 你能上传更新的 web.config 吗?您的 signalR 集线器的位置在哪里?
    • 规则应用如下:应用程序级配置文件中包含的规则优先于继承的规则。系统通过为 URL 构建所有规则的合并列表来确定哪个规则优先,最近的规则(在层次结构中最近的规则)位于列表的开头。给定应用程序的一组合并规则,ASP.NET 从列表的头部开始检查规则,直到找到第一个匹配项。 ASP.NET 的默认配置包含一个 元素,它授权所有用户。 (默认情况下,此规则最后应用。)如果 n
    • 如果没有其他授权规则匹配,则允许该请求。如果找到匹配项并且匹配项是拒绝元素,则返回请求并带有 401 HTTP 状态代码。如果允许元素匹配,则模块允许进一步处理请求。我会建议删除所有规则,然后根据如何应用这些规则的说明逐个添加规则。
    猜你喜欢
    • 2011-11-04
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多