基于用户ROLE的ASP.NET HTML生成

时间:2012-06-10 23:08:36

标签: asp.net

我开发了非常简单的网站,我有三个网页。

  1. Logon.aspx
  2. Register.aspx
  3. MyAccount.aspx
  4. Logon.aspx代码:

            if (ValidateUser(email, password))
            {
                FormsAuthenticationTicket tkt;
                string cookiestr;
                HttpCookie ck;
                tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now,
                                                    DateTime.Now.AddMinutes(3), chkPersistCookie.Checked,
                                                    email + "@ticket");
                cookiestr = FormsAuthentication.Encrypt(tkt);
                ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
                if (chkPersistCookie.Checked)
                    ck.Expires = tkt.Expiration;
                ck.Path = FormsAuthentication.FormsCookiePath;
                Response.Cookies.Add(ck);
    
                string strRedirect;
                strRedirect = Request["ReturnUrl"];
                if (strRedirect == null)
                    strRedirect = "MyAccount.aspx";
                Response.Redirect(strRedirect, true);
            }
            else
                Response.Redirect("logon.aspx", true);
    

    Register.aspx代码:

        private bool RegisterUser(string name, string surname, string email, string phone, string pass)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=Server\SQL;Initial Catalog=Db;Integrated Security=True; User ID=user; Password =pass;");
            conn.Open();
            string insertQuery = @"INSERT INTO Users (Email, Name, Surname, Phone, Manager, Rank, Password)
                                    VALUES (@Email, @Name, @Surname, @Phone, @Manager, @Rank, @Password)";
            SqlCommand cmd = new SqlCommand(insertQuery, conn);
    
            cmd.Parameters.Add("@Email", email);
            cmd.Parameters.Add("@Name", name);
            cmd.Parameters.Add("@Surname", surname);
            cmd.Parameters.Add("@Phone", phone);
            cmd.Parameters.Add("@Manager", "Test@Test.com");
            cmd.Parameters.Add("@Rank", "1");
            cmd.Parameters.Add("@Password", pass);
    
            try
            {
                int rowsAffected = cmd.ExecuteNonQuery();
            }
            catch (Exception e )
            {
    
                throw;
            }
            finally
            {
                cmd.Dispose();
                insertQuery = string.Empty;
            }
    

    一切都很完美。 现在我想要的是MyAccount.aspx的Page_Load:

    1. 获取Cookie
    2. 检查用户角色
    3. 基于用户角色显示所需的网站内容。
    4. 但我不知道该怎么做。

      1. 我应该参考cookie来建立用户角色吗?
      2. 如何根据用户角色生成HTML?
      3. 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您不希望基于角色生成大量HTML,更像是您希望网站中的内容只能访问某些角色,即不同的页面和控件。 This codeproject page正在做你需要的事情。