如何对asp.net中的登录页面进行身份验证 [英] how to give authentication to login page in asp.net

查看:210
本文介绍了如何对asp.net中的登录页面进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的项目中,输入凭据时,我有login.aspx页和welcome.aspx页
并登录,导航到welcome.aspx页面,在welcome.aspx页面中,我有注销按钮,当我单击注销按钮时,它导航到login.aspx.但是在浏览器中,我单击按钮,它正在导航到welcome.aspx页面.所以我想清除cookie或会话,该怎么做
给我发送示例..

Hi,
in my project i have login.aspx page and welcome.aspx page, when enter the credentials
and logged in, navigated to welcome.aspx page and in welcome.aspx page i have logout button, when i clicked on logout button, it navigated to login.aspx.but in browser i clicked button it is navigating to welcome.aspx page. so i want to clear the cookie or session, how to do this
send me the example..

推荐答案

请参考以下代码,

成功登录后,添加以下代码以在会话中添加用户数据.
Please refer following code,

After successful login add following code to add user data in session.
HttpContext.Current.Session["UserID"] = data.UserId;
        HttpContext.Current.Session["Email"] = data.Email;



在母版页中添加以下代码.



Add following code in master page.

public bool checkAuthentication
    {
        get;
        set;
    } 

protected void Page_Load(object sender, EventArgs e)
    {        
        if (checkAuthentication)
        {
            if ((Session["UserID"] + "") == "")
            {
                Response.Write("<script type=\"text/javascript\">" +
                        "window.parent.location = '" + ConfigurationManager.AppSettings["SiteUrl"] + "login?loginUrl=' + window.parent.location + '&mode=session';" +
                                "</script>");
                Response.End();
            }
        }
}



在要验证用户会话的每个子页面中添加以下代码.



Add following code in each child page where you want to authenticate user session.

protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Master.checkAuthentication = true;
    }



单击注销按钮时,重定向到注销页面.
在logout.aspx的页面加载中添加以下代码



On click of logout button redirect to logout page.
Add following code on page load of logout.aspx

protected void Page_Load(object sender, EventArgs e)
   {
       Session.Abandon();
       Response.Redirect(ConfigurationManager.AppSettings["SiteUrl"] + "login?mode=logout",true);
   }



希望这会对您有所帮助.



Hope this may help you.


拥有一个web.config可以防止用户登录后进入欢迎页面.

或者在welcome.aspx.cs page_load中检查是否没有用户登录到重定向到登录页面.
Have a web.config to prevent the user to access the welcome page unless he is logged in.

alternatively in welcome.aspx.cs page_load check if no user is logged in redirect to login page.


这篇关于如何对asp.net中的登录页面进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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