ASP.NET重定向到登录页面,但不重定向到默认页面 [英] ASP.NET Redirect to login page but not for default page

查看:66
本文介绍了ASP.NET重定向到登录页面,但不重定向到默认页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET 4.5/C#网站,我正在尝试执行以下操作.当用户使用其网站的根目录www.blah.com时,应将其重定向到Default.aspx.此页面是打开的,不需要登录.如果他们要登录,此页面上会有一个链接.但是,如果他们尝试访问其他页面,则需要自动将其重定向为登录.有些对所有人开放,有些则不开放.我很乐意通过每个页面或每个文件夹的角色分配权限.我遇到的问题是Default.aspx.

I have an ASP.NET 4.5 / C# site where I am trying to do the following. When users his the root of the site, www.blah.com, they should be redirected to Default.aspx. This page is open and does not require login. There is a link on this page if they would like to login. However, I need to automatically redirect them to login if they try to access other pages. Some are open to all and some are not. I'm cool with assigning permissions via roles per page or per folder. The issue I have is with Default.aspx.

如果用户通过键入以下内容来访问我的网站:www.blah.com/Default.aspx,则该页面显示的登录名没有重定向.

If a user comes to my site by typing: www.blah.com/Default.aspx then the page comes up with no redirect for a login.

如果用户通过键入以下内容来访问我的网站:www.blah.com/Admin.aspx,则他们将被重定向到登录名.

If a user comes to my site by typing: www.blah.com/Admin.aspx then they are redirected to login.

到目前为止,我的生活还不错,但是...

My life is good so far, but...

如果用户通过键入以下内容来访问我的网站:www.blah.com,那么他们将被重定向到登录名,而不仅仅是加载Default.aspx.这是相关的web.config.

If a user comes to my site by typing: www.blah.com then they are redirected to login instead of just loading Default.aspx. Here is the relevant web.config.

<location path="Default.aspx">
  <system.web>
    <authorization>        
      <allow users="?"/>
    </authorization>
  </system.web>
</location>
<system.web>  
  <authentication mode="Forms">
    <forms name=".ASPXFORMSAUTH" loginUrl="~/Login.aspx" defaultUrl="~/Default.aspx" requireSSL="false" protection="All" slidingExpiration="true" path="/" timeout="240"  cookieless="UseDeviceProfile" enableCrossAppRedirects="false" />     
  </authentication>
  <authorization>
    <deny users= "?"/>
  </authorization>
<system.web>

我假设我对Default.aspx的特定权限将覆盖广义的拒绝.再次,这似乎起作用.我遇到的问题是,当有人访问我网站的根目录www.blah.com时,它会自动重定向到Default.aspx.

I assume my specific permission on Default.aspx overrides the generalized deny. Again, this appears to work. The issue I have is the automatic redirect to Default.aspx when someone hits the root of my site, www.blah.com.

一如既往,谢谢!

更新

update

我已将所有内容移至以下三个目录之一:_public,_private,_admin.现在就不用担心_admin了.这是我的web.config的设置方式:

I have moved everything into one of 3 directories: _public, _private, _admin. Let's not worry about _admin for now. Here is how my web.config is set up:

<defaultDocument>
  <files>
    <clear />
    <add value="~/_public/Default.aspx" />
  </files>
</defaultDocument>
.
.
.
<location path="_public">
  <system.web>
    <authorization>        
      <allow users="?"/>
    </authorization>
  </system.web>
</location>
<location path="_private">
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
</location>
.
.
.
<forms name=".ASPXFORMSAUTH" loginUrl="~/_public/Login.aspx" defaultUrl="~/_private/landingPage.aspx" slidingExpiration="true" path="/" />

我仍然在做错事.我的用户访问www.blah.com时不应该访问〜/_public/Default.aspx吗?现在它告诉我我没有指定默认页面.

I am still doing something wrong. Shouldn't my users come to ~/_public/Default.aspx when they hit www.blah.com? Now it is telling me I don't have a default page specified.

另外,关于表单标签的defaultUrl属性.这如何与我的登录控件的DestinationPageUrl结合在一起?

Also, regarding the defaultUrl property of the forms tag. How does this tie in with the DestinationPageUrl of my login control?

谢谢!

更新 我将位置标记中的路径更新为〜/folder"而不是"folder".现在,使我进入登录屏幕的只是一个错误,而不是一个错误.我在web.config的根目录中没有另一个授权标签,但是我正在处理主web.config中的子文件夹,而不是在每个子文件夹中放置一个单独的文件.

update I updated the paths in the location tag to "~/folder" instead of "folder". Now instead of an error is just keeps taking me to the login screen. I don't have another authorization tag in the root of web.config, but I am handling the subfolders from the main web.config instead of putting a separate file in each subfolder.

推荐答案

谢谢大家的详细答复.他们都非常有帮助,让我提出了一个更好的整体解决方案.但是,我的问题的根源是很小而烦人的事情.再次查看我的默认页面:

Thank you all for the detailed replies. They were all very helpful and let me to a better overall solution. However, the root of my issue was something very small and annoying. Take a look at my default page again:

<defaultDocument>
  <files>
    <clear />
    <add value="~/_public/Default.aspx" />
  </files>
</defaultDocument>

注意值中的路径.我对此进行了实验,发现如果我以波浪号或正斜杠开头,则该值将被完全忽略.一旦我将其指定为"_public/Default.aspx",其他所有内容就都可以正常使用并正常工作了.

Notice the path in the value. I experimented with this and found that if I lead with a tilde or a forward slash that this value is completely ignored. Once I specified it as "_public/Default.aspx" everything else fell into place and worked as expected.

我确实做了一些改变.我正在为页面使用三个子文件夹:_public,_private和_admin._public适用于任何人,_private适用于经过身份验证的任何人,_admin适用于具有特殊privs的人员.我想提一提,我的网站根目录中没有任何Default.aspx文件.为了验证我在web.config中指定路径的方式是错误的,我尝试以完全相同的错误将其切换回去.

I did change a few things up though. I am using three subfolders for pages: _public, _private, and _admin. _public is for anyone, _private is for anyone authenticated, and _admin is for those with special privs. I want to mention that I DO NOT have any Default.aspx file in the root of my website whatsoever. To verify the way I was specifying the path in the web.config was wrong, I have tried switching it back with exactly the same failure.

我很好奇是否有人发现了这一点,您是否认为它前后矛盾?在web.config的其他大部分地方,我都可以通过波浪号开头来指定从根开始的路径.

I'm curious if anyone has spotted this, and do you see it as inconsistent? Most everywhere else in my web.config I can specify a path from root by beginning with a tilde.

因此,我的网站按预期运行...匿名用户访问公共页面效果很好.点击私人或管理页面会重定向到登录名.登录后,如果您尝试进入某个特定页面,请转到该页面...否则,将通过forms标记中的defaulturl进行重定向.最重要的是,当您输入未指定页面的站点名称时,您将被带到默认页面...如果不是语法问题,这应该是所有页面中最简单的部分.就是这样...

So, my site acts as expected...anonymous users hitting public pages works fine. Hitting private or admin pages redirects to login. Once logged in, if you were trying to get to a specific page you go there...if not, you are redirected via the defaulturl in the forms tag. Most importantly, when you type in the name of the site with no page specified, you are taken to the default page...which should have been the simplest part of it all if not for the syntax issue. So there it is....doh!

这篇关于ASP.NET重定向到登录页面,但不重定向到默认页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆