如何在登录页面中使用会话 [英] how to use session in login page

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

问题描述

通过这个代码,我用来登录,但这不是有效的apporach.i想要使用session我的login.how我会这样做

请帮帮我...



by this code i am using to login but this is not efficient apporach.i want to use session for my login.how would i do it
please help me with that...

try
            {
                SqlConnection con = new SqlConnection(@"Data Source=LENOVO-74FE9906\SQLEXPRESS;AttachDbFilename=D:\Farrukh\Orignal Project\Inventory Management System\IMS.mdf;Integrated Security=True");
                string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
                SqlCommand com = new SqlCommand(query, con);
                con.Open();
                if (txtusername.Text == "admin")
                {

                    if (com.ExecuteReader().HasRows)
                    {


                        Response.Redirect("index.html");
                        Label3.Text = "successfully";



                    }
                    else
                    {
                        Label3.Text = "not successfully";
                        txtusername.Text = "";
                        txtpassword.Text = "";
                    }
                }
                else if (txtusername.Text == "user")
                {
                    if (com.ExecuteReader().HasRows)
                    {


                        Response.Redirect("userinventory.aspx");
                        Label3.Text = "successfully";



                    }
                    else
                    {
                        Label3.Text = "not successfully";
                        txtusername.Text = "";
                        txtpassword.Text = "";
                    }
                }

                con.Close();
            }
            catch (Exception ex)
            {
                Console.Write("" + ex.Message);
            }

推荐答案

添加此



会话[用户名]





用户成功登录的位置....
Add this

Session["username"]


to where the user in succesfully logiin ....


First总而言之,如果您使用会话,它将存储在服务器内存中,所以这是个坏主意,作为一个优秀的软件专家,您应该避免使用它。



此外,请参阅到以下链接。



使用角色管理网站用户 [ ^ ] [此链接将说明如何根据用户的角色重定向用户(管理员,用户等)]



实施简单表单身份验证 [ ^ ] [此链接将说明如何将未经身份验证的用户重定向到登录页面]



我强烈建议您使用基于角色的身份验证,因为,考虑具有20种用户类型的系统(admin,user,teacher,hod,ass prof。教授。等)然后将每个用户重定向到不同的页面将是如此乏味。



所以尝试使用上述方法,这将有助于您在此以及将来问题也是。



最好的问候.. :)
First of all,if you use session it will be stored in server memory,so thats bad idea and as a good software expert you should avoid using that.

Further,refer to the below links.

Managing Web Site Users with Roles[^] [This link will explain how to redirect users based on their role(admin,user etc.)]

Implement Simple Forms Authentication[^] [This link will explain how to redirect unauthenticated user to login page]

I strongly suggest you to use role based authentication because,think of the system which have 20 user types(admin,user,teacher,hod,ass prof. prof. etc) then redirecting each and every user to different pages will be so tedious.

So try using the above approach,that will help you in this as well as in future issues also.

Best Regards.. :)


嗨...

使用会话像这样。



session [username] = textbox1.text;

session [password] = textbox2.text;



如果你想要任何地方,创建obj登录页面并使用会话。

谢谢你。
Hi...
Use sessions like this.

session["username"]=textbox1.text;
session["password"]=textbox2.text;

if u want any where,create obj to login page and use sessions.
thank u.


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

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