使用ASP.NET成员重定向用户多张登录页面 [英] Redirect user to Mulitple Login Pages using ASP.NET Membership

查看:92
本文介绍了使用ASP.NET成员重定向用户多张登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重定向用户到登录页面依赖于它们在文件夹中。我有它用于所有用户的根目录和管理网站的Web应用程序。

有关这将需要该网站的认证功能的人,他们将需要登录并重定向到根/ login.aspx的。但是,当管理员需要登录到该站点的根目录/管理/节,我希望他们被重定向到根/管理/ login.aspx的

登录表单

 <结构>
      <的appSettings />
      <是connectionStrings />
    <&的System.Web GT;
      <授权>
        <拒绝用户=? />
      < /授权>
    < /system.web>
  < /结构>

我在根/ admin目录此文件。我曾尝试添加以下行,但它给错误。

 <身份验证和GT;
    <形式defaultUrl =的Default.aspxloginUrl =Default.aspx的>< /形式GT;
  < /认证>

基本上我试图覆盖存在的主要应用程序的defaulturl和loginurl。


解决方案

您需要使用<地点> 元素在你的web.config。您可以使用<地点> 标签授权设置应用于单个文件或目录。

 <位置路径=/根>
  <&的System.Web GT;
      <身份验证模式=表格>
        <形式的名称=LoginForm的defaultUrl =的Default.aspx
        loginUrl =/根/ login.aspx的保护=加密
        超时=30路径=//>
      < /认证>
    <授权>
      <让用户=? />
    < /授权>
  < /system.web>
< /地点>
<位置路径=/根/ admin的>
  <&的System.Web GT;
    <身份验证模式=表格>
      <形式的名称=窗体名称defaultUrl =login.aspx的
      loginUrl =/根/管理/ login.aspx的保护=加密
      超时=30路径=//>
    < /认证>
    <授权>
      <让用户=? />
    < /授权>
  < /system.web>
< /地点>

MSDN


  

有关集中管理,
  设置可以在应用
  Machine.config文件。在设置
  Machine.config文件中定义
  机器范围的策略,也可以是
  用于施加特定于应用程序
  使用配置<地点>
  元素。开发者可以提供
  应用程序配置文件
  覆盖计算机策略的各方面。
  对于ASP.NET Web应用程序中,
  Web.config文件位于
  应用程序的虚拟根目录
  和可选的子目录
  下面的虚拟根目录。


如果你想,你可能想使用的角色的。

 <位置路径=/根>
  <&的System.Web GT;
    <授权>
       <让角色=管理员,根/> / *管理,允许root * /
       <拒绝用户=*/>
   < /授权>
  <&的System.Web GT;
< /地点><位置路径=/根/ admin的>
  <&的System.Web GT;
    <授权>
       <让角色=管理员/> / *管理员允许* /
       <拒绝用户=*/>
   < /授权>
  <&的System.Web GT;
< /地点>


  

用户可以属于一个以上的
  角色。举例来说,如果你的网站是一个
  论坛中,一些用户可能会
  中两个成员的作用,
  版主。您可以定义每个角色
  有对不同的权限
  网站,谁是这两个角色的用户
  那么将有两套
  特权。


  
  

您可以访问所有这些元素
  在code级,如果你想
  操纵角色/认证
  编程


  Page.User.Identity.Name
Page.User.Identity.IsAuthenticated
Page.User.Identity.AuthenticationType
Page.User.IsInRole(字符串);

教程

4家伙从罗拉教程

的ASP.NET Web.config文件揭秘

Redirect user to Login Page dependent on the Folder they are in. I have a web application with the root directory which is used by all users and the admin site.

For people that would require the authenticated functionality of the site, they would require to login and be redirected to root/login.aspx. However, when an Admin needs to login to the root/admin/ section of the site, I want them to be redirected to the login form on root/admin/login.aspx

  <configuration>
      <appSettings/>
      <connectionStrings/>
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </configuration>

I have this file in the root/admin directory. I have tried adding the following line but it is giving an error.

  <authentication>
    <forms defaultUrl="default.aspx" loginUrl="default.aspx"></forms>
  </authentication>

Basically I am trying to overwrite the defaulturl and loginurl that exists in the main app.

解决方案

You need to use the <location> element in your web.config. You can use the <location> tag to apply authorization settings to an individual file or directory.

<location path="/root">
  <system.web>
      <authentication mode="Forms" >
        <forms name="LoginForm" defaultUrl="default.aspx" 
        loginUrl="/root/login.aspx" protection="Encryption" 
        timeout="30" path="/"/>
      </authentication>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>
<location path="/root/admin">
  <system.web>
    <authentication mode="Forms" >
      <forms name="formName" defaultUrl="login.aspx" 
      loginUrl="/root/admin/login.aspx" protection="Encryption"
      timeout="30" path="/"/>
    </authentication>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>

MSDN

For centralized administration, settings can be applied in the Machine.config file. The settings in the Machine.config file define machine-wide policy and can also be used to apply application-specific configuration using <location> elements. Developers can provide application-configuration files to override aspects of machine policy. For ASP.NET Web applications, a Web.config file is located in the application's virtual root directory and optionally in subdirectories beneath the virtual root.

If you would like 1 login location and different access levels you might want to use roles.

<location path="/root">
  <system.web>
    <authorization>
       <allow roles="admin,root" />/*admin, root is allowed */
       <deny users="*" /> 
   </authorization>
  <system.web>
</location>  

<location path="/root/admin">
  <system.web>
    <authorization>
       <allow roles="admin" />/*admin is allowed */
       <deny users="*" /> 
   </authorization>
  <system.web>
</location>

Users can belong to more than one role. For example, if your site is a discussion forum, some users might be in the role of both Members and Moderators. You might define each role to have different privileges on the site, and a user who is in both roles would then have both sets of privileges.

You can access all these element at the code level if you would like to manipulate the roles/authentication programmatically

Page.User.Identity.Name
Page.User.Identity.IsAuthenticated
Page.User.Identity.AuthenticationType
Page.User.IsInRole("string");

Tutorials

4 Guys From Rolla Tutorial

The ASP.NET web.config File Demystified

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

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