简体   繁体   English

使用带有重写URL的表单身份验证/授权

[英]Using Forms Authentication/Authorization with Rewritten URLs

I'm doing a quick sandbox test with some Rewritten URLs (example taken from Scott Guthrie's blog) and Forms Authentication / Authorization. 我正在使用一些重写URL进行快速沙盒测试(例子来自Scott Guthrie的博客)和表单身份验证/授权。

I've a very simple setup. 我的设置非常简单。

~/View/(\d{1,6})      =>      ~/Public/View.aspx?ContentID=$1

AND

~/Buy/(\d{1,6})       =>      ~/Private/Purchase.aspx?ContentID=$1

I've confirmed the URL Rewriting is working by browsing to each of the following seperately 我已经确认URL重写正在通过单独浏览以下各项来工作

Next I went and enabled my Forms Authentication/Authorization for those 2 directories in the Web.Config. 接下来,我开始为Web.Config中的这两个目录启用表单身份验证/授权。 Setup as follows 设置如下

  <location path="Private">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>
  <location path="Public">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location> 

This works perfectly when I browse to the 2 original URLs (the .aspx's) but doesn't fire at all when I browse to the URL Rewritten versions. 当我浏览到2个原始URL(.aspx)时,这种方法非常有效,但当我浏览到URL重写版本时,它根本不会触发。

I've attempted to add <location> sections for Buy seperately but this still fails to cause the authorization/authentication module to kick in. 我试图单独为Buy添加<location>部分,但这仍然无法导致授权/身份验证模块启动。

Presumably this is because it isn't treating these URLs as ASPX Resources... I can get around it by making the rewriter rule look for 可能这是因为它没有将这些URL视为ASPX资源...我可以通过使重写器规则查找来解决它

    <LookFor>~/Buy/(\d{1,6})\.aspx</LookFor>

ie force the rewritten version to have an ASPX at the end, but this just seems ugly. 即强制重写的版本最后有一个ASPX,但这看起来很难看。 Is there anyway to get the Auth Handlers to fire for any url type regardless of the extension (or lack there of) 无论如何都要让Auth处理程序为任何URL类型触发,无论扩展名(或缺少扩展名)

To use built-in auth, you will have to decide whether you want to authenticate based on the original 'raw' URLs or the rewritten ones. 要使用内置身份验证,您必须根据原始“原始”URL或重写的URL来决定是否要进行身份验证。 It appears as if the URL rewriter you're using is hooked up to an event after the authentication has already been performed, which is why only the 'Public' and 'Private' folder rules are being followed. 看起来好像您正在使用的URL重写器已在执行身份验证后连接到事件,这就是为什么只遵循“公共”和“私有”文件夹规则的原因。 If you want to authenticate based on the rewritten URLs, then you'll have to use a rewriter that hooks up to an earlier event (such as BeginRequest) as well as updating your web.config with the rewritten URLs. 如果要根据重写的URL进行身份验证,则必须使用连接到早期事件(例如BeginRequest)的重写器以及使用重写的URL更新web.config。

Alternatively, you can plug-in your own authentication provider and do fancy things like checking both rewritten and original URLs, but that's probably overkill for just a sandbox test site. 或者,您可以插入自己的身份验证提供程序,并执行诸如检查重写和原始URL之类的奇特事情,但这对于沙盒测试站点来说可能有点过分。

Please see this article for more information: 有关更多信息,请参阅此文章:

http://msdn.microsoft.com/en-us/library/ms972974.aspx http://msdn.microsoft.com/en-us/library/ms972974.aspx

I hope this helps. 我希望这有帮助。

In ASP.NET 4.0 (and I believe it is in 3.5 SP1), there is included a new routing feature. 在ASP.NET 4.0中(我相信它在3.5 SP1中),包含了一个新的路由功能。 The benefits of using this routing feature is that it is now supported directly inside ASP.NET, and you can therefore specify that when a route is executed, it shall respect the authorization settings for the actual .ASPX file. 使用此路由功能的好处是现在可以直接在ASP.NET中支持它,因此您可以指定在执行路由时,它应该遵守实际.ASPX文件的授权设置。

So I would reccomend you to investigate if you can implement this routing feature instead. 因此,我建议您调查是否可以实现此路由功能。

Its not clear what url rewriting library you use but from the looks of things I think its probably urlrewriter.net however I dont see any tag? 它不清楚你使用的url重写库,但从我认为它可能urlrewriter.net的东西的外观,但我没有看到任何标记?

try enabling formsauthentication and authorisation on all requests. 尝试对所有请求启用formauthentication和authorization。 By default it is only enabled for asp.net requests like .aspx. 默认情况下,它仅对.aspx等asp.net请求启用。 Can be done in IIS (7) or directly in web.config in webserver/modules section 可以在IIS(7)中完成,也可以直接在webserver / modules部分的web.config中完成

<system.webServer>
   <modules>
        <remove name="FormsAuthentication" />
        <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" preCondition=""/>
        <remove name="UrlAuthorization" />
        <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" preCondition="" />
   </modules>
</system.webServer>

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

相关问题 使用表单身份验证时如何检查授权? - How can I check for authorization when using forms authentication? 以编程方式生成重写的URL? - Programmatically Producing rewritten URLs? ASP.NET Forms身份验证的授权安全性 - Authorization security of ASP.NET Forms authentication 设置RAMMFAR时,为什么IIS 7在Web窗体回发中使用重写的URL(Helicon)剥离请求参数? - Why does IIS 7 strip request parameters on Web Forms postback with rewritten urls (Helicon) when RAMMFAR is set? 使用ASP.NET角色的WCF表单身份验证授权=访问被拒绝? - WCF Forms Authentication authorization using ASP.NET roles = Access Denied? 使用表单身份验证类进行身份验证 - Using forms authentication class for authentication 使用OWIN实现身份验证和授权 - Implementing authentication and authorization using OWIN 使用表单身份验证进行模拟 - Impersonate using Forms Authentication 如何使用Forms身份验证在ASP.NET Web应用程序中为动态创建的资源配置用户授权? - How do I configure user authorization on dynamically created resources in an ASP.NET web app using Forms authentication? ASP.NET为下游WCF身份验证和授权形成身份验证 - ASP.NET Forms Auth For Downstream WCF Authentication and Authorization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM