在C#ASP.net中管理用户会话 [英] Managing user session in C# ASP.net

查看:102
本文介绍了在C#ASP.net中管理用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个有登录页面的网站。只要有效用户登录,它就会被重定向到应用程序页面。现在我希望它有一个注销选项。我已经使用重定向到登录页面并禁用后退按钮以及通过擦除浏览器的浏览历史来完成此操作。现在我希望使用session管理它。



请帮助使用代码片段。





谢谢....

Hi,

I have a website where there is a login page. Whenever a valid user makes login, it is redirected to application pages. Now I want it to have a sign out option.I have done this using redirecting to login page and disabling the back button and also by erasing browsing history of the browser. Now I want this to be managed using session.

Please help with code snippet.


Thanks....

推荐答案

试试登录.... :)



Try this for login....:)

protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUsername.Text != "" && txtPassword.Text != "")
        {
            RegistrationUser Dataservice = new RegistrationUser();
            DataTable dtGetlogDetail = Dataservice.GetLoginDetail(txtUsername.Text, txtPassword.Text);
            if (dtGetlogDetail.Rows.Count > 0)
            {
                if (dtGetlogDetail.Rows[0]["confirmed"].ToString() == "1")
                {
                    Session["User"] = Convert.ToInt32(dtGetlogDetail.Rows[0]["id"]);
                    Response.Redirect("~/Customer/ClientIndex.aspx");
                }
            }
            else
            {
                Response.Write("<script>alert('Please enter valid Password or Username.')</script>");
            }
        }
    }



尝试使用Loguot .... :)


Try this for Loguot....:)

protected void btnLogOut_Click(object sender, EventArgs e)
{
 if (Session["User"] != null)
        {
            Session.Clear();//clear session
            Session.Abandon();//Abandon session
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();          
        }
}


试试这个。 FormsAuthentication.SignOut Method [ ^ ]


不建议使用会话实现登录功能,因为有优点。和利弊。会话如



-Session将在服务器端处理。

- 如果工作进程或应用程序域被回收,则所有会话数据将丢失。

- 尽管它是最快的,更多的会话数据和更多的用户可能会因为内存使用而影响性能。

-参与序列化的Deverhead和De -Serializing session数据。因为在StateServer和SQLServer会话模式的情况下,我们需要在存储之前序列化对象。

-会话有过期行为。如果过期的应用程序将崩溃。



强烈建议使用以下方法。在可靠性和维护方面会更好。



如何:对SQL Server使用表单身份验证 [ ^ ]



ASP.NET中的表单身份验证和授权 [ ^ ]



如何:实现简单表单身份验证 [ ^ ]



[A. dditional]:



通过ASP.NET中的角色验证用户 [ ^ ]



问候......:笑:
Well,implementing login functionality using session is not recommended as there are pros. and cons. with session such as

-Session will be handled at server side.
-If the worker process or application domain is recycled, all session data will be lost.
-Though it is the fastest, more session data and more users can affect performance, because of memory usage.
-Overhead involved in serializing and De-Serializing session Data. because In case of StateServer and SQLServer session mode we need to serialize the object before store.
-Sessions have expiration behavior. If expired app would be crashed.

It is strongly recommended to use below approaches.It would be better in terms of reliability as well as maintainance.

How To: Use Forms Authentication with SQL Server[^]

Form authentication and authorization in ASP.NET[^]

How to: Implement Simple Forms Authentication[^]

[Additional]:

Authenticate User by Roles in ASP.NET[^]

Regards.. :laugh:


这篇关于在C#ASP.net中管理用户会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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