如何覆盖某些网页的身份验证....?
好吧,我在母版页中编写了以下代码: -
<authentication mode="Forms">
<forms loginUrl="Loginpage.aspx" />
</authentication>
现在,如果身份验证失败,它将重定向到“Loginpage.aspx”。
现在,如果我想覆盖几页的此身份验证,该怎么办?另请注意,页面数和页面名称在设计时不可用,因此不能在配置文件中包含 aspx 页面名称。
有没有办法覆盖少数 aspx 页面的身份验证?
-阿尼尔
Well I am having following code written in master page: -
<authentication mode="Forms">
<forms loginUrl="Loginpage.aspx" />
</authentication>
Now it will redirect to "Loginpage.aspx" if authentication fails.
Now what If I would like to override this authentication for few pages. Also note that the number of pages and page names are not available at design time, so cannot include the aspx page names in configuration file.
Is there anyway to override authentication for few aspx pages?
-Anil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Henrik 的答案是一个很好的答案,如果实施得当,应该会起作用。然而,这是另一个从配置角度更多地解决问题的选项。我知道您提到您不会提前知道页面名称,因此您无法在每个页面的 web.config 中包含一个条目,但 web.config 也允许您保护文件夹。例如,您可以将所有需要身份验证的页面放置在名为“AuthRequired”的文件夹中,并将所有不需要身份验证的页面放置在名为“Anonymous”的文件夹中。然后在您的网络配置中您可以有以下条目:
Henrik's answer is a good one and should work if properly implemented. However, this is another option which tackles the problem more from a configuration standpoint. I know that you mentioned that you won't know the page names ahead of time so you can't include an entry in web.config for each page BUT web.config allows you to secure folders too. You could have all pages that require authentication placed in a folder called "AuthRequired" and all pages that don't require authentication placed in a folder called "Anonymous", for example. Then in your web config you could have the following entries:
您可以监听 AuthorizeRequest 事件并采取相应的行动。创建您自己的 Http 模块来执行此操作。
三个选项:
使用上面的配置设置以及生成带有 web.config 条目的文件夹。这是一种相当糟糕的方法。
监听事件AuthenticateRequest,代码如下:
如果您的 UserPrincipal 实现了 IPrincipal,则 IsInRole 用于提供对页面的基于角色的访问。
您遵循的原则是,如果 Web 应用程序中不允许某些操作,您会抛出 new HttpException(405, "您尝试执行的当前操作现在对于您的角色或用户或选择的生活路径是允许的" ) 在 AuthorizeRequest 事件中。 请注意,有一个 AuthenticateRequest 和另一个 AuthorizeRequest 事件
You can listen to the AuthorizeRequest event and act accordingly. Create your own Http Module to do this.
Three options:
use the configuration settings above together with generating folders with web.config entries. This is a pretty shoddy way of doing it.
listen to the event AuthenticateRequest, the code looks something like this:
If your UserPrincipal implements IPrincipal, then IsInRole is used to give role-based access to your pages.
The principle that you follow is that if something is not allowed in a web application you do
throw new HttpException(405, "The current operation you are trying to perform is now allowed for your role or user or chosen path in life")
in the AuthorizeRequest event. Note that there's a AuthenticateRequest and another AuthorizeRequest event您通常应该有一个点来对用户进行身份验证 - 确认他们的身份。接下来,您可能正在谈论授权,这是允许/拒绝对用户执行某些操作的问题,例如发送 GET 请求。简单场景中的授权规则可以通过 location 元素在 web.config 中配置,如 Tom 所示。
You should usually have one point where users can be authenticated - get confirmed that they are who they claim they are. Next, you are probably talking about authorisation, which is a matter of allowing/denying performing certain operation to the user, like sending a GET request. Authorisation rules in a simple scenarios can be configured in the web.config through location element, as presented by Tom.