在asp.net中限制用户访问

时间:2014-06-25 08:19:57

标签: asp.net sql

我正在开发asp.net应用程序。我只想登录用户访问游戏页面。当用户登录时,id和pass通过SQL进行身份验证,然后登录。我希望登录用户可以访问Games.aspx。

这是登录代码,

  public partial class Login : System.Web.UI.Page
  {
    //"Data Source=MUNIZA\\SQLEXPRESS;Initial Catalog=LD_Server;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        lbInfo.Enabled = false;
    }

    public bool IsAuthenticated
    {
        get { return Convert.ToBoolean(Session["sIsAuthenticated"] ?? false); }
        set { Session["sIsAuthenticated"] = value; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
                    string strcon = "Data Source=MUNIZA\\SQLEXPRESS;Initial Catalog=LD_Server;Integrated Security=True";

        SqlConnection con = new SqlConnection(strcon);



        SqlCommand com = new SqlCommand("spStudentProfile", con);

        com.CommandType = CommandType.StoredProcedure;

        SqlParameter p1 = new SqlParameter("RegNo", TextBox2.Text);

        SqlParameter p2 = new SqlParameter("Password", TextBox1.Text);


        com.Parameters.Add(p1);

        com.Parameters.Add(p2);

        con.Open();

        SqlDataReader rd = com.ExecuteReader();

        if (rd.HasRows)
        {
            IsAuthenticated = true;
            rd.Read();

            Response.Redirect("~/Games.aspx");

        }



        else
        {
            IsAuthenticated = false;
            lbInfo.Enabled = true;
            lbInfo.Text = "Invalid username or password.";


        }
    }

这是每个页面上的登录代码,

                     <% 
  string url = "~/Login.aspx", text = "Log in";
  if (Convert.ToBoolean(Session["sIsAuthenticated"] ?? false))
 { url = "~/Home.aspx"; text = "Log out"; }
  %>
  <a href="<%: ResolveUrl(url) %>"><%: text %></a>
            </div>

0 个答案:

没有答案