代码之家  ›  专栏  ›  技术社区  ›  ecleel

什么是使ASP.NET中的某些页需要登录的最佳方法?

  •  6
  • ecleel  · 技术社区  · 17 年前

    我正在使用ASP.NET网站项目,其中一些页面需要身份验证。我正在使用ASP.NET成员身份。

    我读了一些答案。例如,在文件夹中创建所有这些页面,并创建描述特权的内部web.config。这是解决问题的一种方法,但我需要一种更为固定和有效的方法。

    2 回复  |  直到 14 年前
        1
  •  20
  •   Zhaph - Ben Duguid    17 年前

    如果不想在web.config中硬编码,则需要实现“基本页”类型控件。

    您的基本页类应该继承自System.Web.UI.Page,并且需要有一个可以调用的方法来表示“用户必须登录”或“用户必须在角色X中”,如果用户不在该角色中,请重定向到登录页(您可以通过调用 FormsAuthentication.LoginUrl )

    您的实际页面应该继承自此类,而不是直接继承自System.Web.UI.Page。然后,在init或页面加载顶部调用

    base.UserMustBeLoggedIn();
    

    或

    // Replace "AccessRole" with the name of your role
    base.UserMustBeInRole("AccessRole");
    

    让基本页处理这个问题。

    如果您希望将访问权限存储在数据库中,那么可以将所有处理移到基本页,并在页面生命周期的适当位置,根据数据库表检查当前URL,根据要求检查用户角色/身份验证,并根据需要重定向。


    请注意,您可以在Web配置中创建页面级安全性,如下所示:

    <configuration>
      <location path="LockedPage.aspx">
        <system.web>
          <authorization>
            <!-- Deny access to anonymous users -->
            <deny users="?"/>
          </authorization>
        </system.web>
      </location>
    </configuration>
    

    有关详细信息,请访问msdn: The Location Element 和 The Authorization Element .

        2
  •  3
  •   Akhil    14 年前

    你可以试试这个代码, 在母版页加载事件中编写此代码, 添加属性

    public bool m_bloginRequired=true;

    public bool IsLoginRequired
    {
        get { return m_bLoginRequired; }
        set { m_bLoginRequired = value; }
    }
    
    
    
    try
            {
                // Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                Response.Cache.SetNoStore();
                if (IsLoginRequired==true)
                {
                        if ( Session.IsNewSession  || HttpContext.Current.Session["Username"] == null)
                        {
                            FormsAuthentication.SignOut();
                            FormsAuthentication.RedirectToLoginPage("Session Expired");
                            Response.End();
                        }
                    }
                }
            catch (Exception ex)
            {
                throw (ex);
            }
    

    现在,在登录页面中,您需要编写此代码

    FormsAuthentication.SetAuthCookie(this.txt_UserName.Text.Trim(), false);  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, this.txt_UserName.Text.Trim(), DateTime.Now, DateTime.Now.AddMinutes(10), false, "HR");
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,  FormsAuthentication.Encrypt(ticket));
                cookie.Name = "jay";
                Session["UserName"] = txt_UserName.Text.Trim();
                Response.Cookies.Add(cookie);
                txt_UserName.Text = "";
                txt_Password.Text = "";
                Response.Redirect("HomePage2.aspx");
    

    现在您必须在登录页面中添加pageinit事件

    protected void Page_PreInit(object sender, EventArgs e)
        {
            Master.IsLoginRequired = false; 
        }
    

    如果您希望用户可以访问未授权的页面,那么 在该页的pageInit事件中

    设置 Master.IsLoginRequired=false;

    还可以在 web.config 文件。

    推荐文章