如何重写/在某些情况下改变FormsAuthentication LoginUrl重写、情况下、在某些、LoginUrl

2023-09-02 10:32:33 作者:稚遇

有没有办法来动态改变FormsAuthentication的LoginUrl?我所拥有的是FormsAuth保护整个网站,而是在子文件夹中的某些页面,我想将用户带到不同的登录页面,并有FormsAuth处理RETURNURL的东西。这是可能的还是我写我自己的重定向$ C $下的子文件夹的情况?

Is there a way to dynamically change the LoginUrl of FormsAuthentication? What I have is the whole site protected by FormsAuth, but for some pages in a sub folder, I'd like to take the user to different login page, and have FormsAuth handle the ReturnUrl stuff. Is that possible or do I have to write my own redirect code for the sub folder cases?

下面是一个例子布局:

   ~/LogOn1.aspx
   ~/Protected1.aspx
   ~/Protected2.aspx
   ~/Subfolder/
   ~/Subfolder/LogOn2.aspx
   ~/Subfolder/NotProtected.aspx
   ~/Subfolder/Protected3.aspx

所以,我的web.config是这样的:

So my web.config looks like:

 <forms loginUrl="~/Splash.aspx" ... />

所有的保护* .aspx页有

All of the Protected*.aspx pages have

 <deny users="?">

我想虽然是〜/子文件夹/ Protected3.aspx被重定向到〜/子文件夹/ LogOn2.aspx如果用户是匿名的。

What I'd like though, is for ~/Subfolder/Protected3.aspx to be redirected to ~/Subfolder/LogOn2.aspx if the user is anonymous.

我也尝试把web.config中的一个精简版的〜/子文件夹/ web.config中:

I did try putting a stripped down version of web.config at ~/Subfolder/web.config:

<?xml version="1.0"?>
<configuration>
   <system.web>
      <authentication mode="Forms">
         <forms loginUrl="~/Subfolder/LogOn.aspx" name="SiteAuth" protection="All" timeout="30" path="/" defaultUrl="~/Subfolder/default.aspx" requireSSL="true" cookieless="UseCookies" enableCrossAppRedirects="false" />
      </authentication>
      <authorization>
         <deny users="?" />
      </authorization>
   </system.web>
</configuration>

不过这些都让我这个错误是:

But all that gets me is this error:

有使用的节是错误   注册为   allowDefinition的='MachineToApplication'的   超越应用水平。此错误   可以通过虚拟目录引起   没有被配置为应用程序   在IIS中。

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

我觉得做的子文件夹目录的应用程序会导致在这一点上,甚至更多的问题,但也许我错了。如果它是一个应用程序,也不会分开,从父应用程序的其余全部code在〜/子文件夹?

I think making the Subfolder dir an application would cause even more problems at this point, but maybe I am wrong. If it was an application, wouldn't that separate all code in ~/Subfolder from the rest of the parent app?

推荐答案

您遇到的问题是,的表单元素只允许在应用层面 - 你不能在一个子web.config中定义它

The problem you're having is that the Forms element is only allowed at the application level - you can't define it in a sub-web.config.

不幸的是,你也可以不使用位置元素定义它,而FormsAuthentication.LoginUrl属性是只读的。

Unfortunately you also can't define it using a Location element, and the FormsAuthentication.LoginUrl property is read only.

了一下周围狩猎,它看起来像你最好的选择将是对你的登录页一些code检测,用户已经从抵达(即通过检查RETURNURL查询字符串的值)重定向到其他登录页面,如果他们是从子目录。不过我承认,这并不在所有的,如果你想自定义登录页面多个子目录很好地扩展。 :(

Hunting around a bit, it looks like your best bet would be to have some code on your login page that detects where the user has arrived from (i.e. by checking the value of the "ReturnUrl" query string) and redirecting to your other login page if they are from the subdirectory. However I admit that this doesn't scale well at all if you want custom login pages for multiple sub-directories. :(

在repsonse您的编辑 - 是的,使得子文件夹中的应用程序将解决这个错误,但正如你指出,你会再有更多的问题,因为你需要将所有相关的二进制文件,APP_ code,你有什么成子文件夹一样,所以它不是一个真正的解决方案。

In repsonse to your edit - yes, making the sub-folder an application would "solve" this error, but as you point out, you'd then have more problems, as you'd need to move all the relevant binaries, app_code, what have you into that sub-folder as well, so it's not really a solution.