如何使登录页面认证的所有页面 [英] How to make all the pages authenticated with the login page

查看:147
本文介绍了如何使登录页面认证的所有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目5 的.aspx 页,第一个是登录页面等是家庭,指数等的问题是,当用户输入用户在登录页面ID和密码,它会直接到下一个页面就是主页。但是,当我干脆把主页的网址也没有任何登录验证显示页面。

I have five .aspx pages in my project, first one is the login page and other are home, index, etc. The problem is that when user enter the user ID and password in the login page, it will direct to next page that is home page. But when I simply put the URL of the home page it also display the page without any login authentication.

推荐答案

为您的项目目前的结构,你可以验证后创建登录页面上的会话变量登录用户的详细信息,用户凭证和存储即

for the current structure of your project, you can create a Session variable on the login page after verifying the user credentials and store logged in user details i.e.

在登录页面,登录按钮单击处理程序做到这一点:

in your login page, login button click handler do this:

  protected void btnLogin_Click(object sender, EventArgs e) 
 {
      string username= txtUsername.Text;
      string pwd = txtPassword.Text;

      //call your logic to verify user credentials.
      VerifyUserCredentialFromDb(username, pwd);

     if(UserValid)
     {
        Session["User"] = GetUserObject(username,pwd);

        //whatever your logic is, make sure, you create the Session object, before 
        //below line,whereever you are doing it
        Response.Redirect("Home.aspx");
     }
 }

和在所有其他网页的在Page_Load

and in the Page_load of all the other pages

  protected void Page_Load(object sender, EventArgs e)
  {
      if(Session["User"]==null)
        Response.Redirect("login.aspx");
  }

顺便说一句,你应该看看的表单验证

的简单窗体身份验证实施

Look at this simple forms auth implementation

这篇关于如何使登录页面认证的所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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