如何使用存储过程在asp.net c#中创建登录页面 [英] How to create login page in asp.net c# using stored procedures

查看:93
本文介绍了如何使用存储过程在asp.net c#中创建登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用存储过程在asp.net c#中创建登录页面。



在这个项目中,我有4个级别的访问权限

1)管理员

2)用户

3)主管

4)TechTeam



请帮我看看如何使用存储过程创建登录。



谢谢

How to create login page in asp.net c# using stored procedures.

In this project I have 4 levels of access
1)Admin
2)User
3)Supervisor
4)TechTeam

Please help me how to create Login using stored procedure.

Thanks

推荐答案

识别用户在任何应用程序开发中,身份验证都是一项非常重要的任务,我经常阅读论坛帖子,询问如何使用存储过程创建登录页面。因此,考虑到上述要求,我决定写这篇文章来帮助初学者,学生以及想要学习如何使用存储过程创建登录页面。



所以让我们从演练开始。



首先在SQL数据库中创建一个名为Emp_Login的表,其中包含两列用户名和密码:

create表Emp_Login(userName nvarchar(50),密码nvarchar(50))





在表格中插入一条记录:

插入Emp_Login(userName,Password)值('vijay','yadav')





在上面的表中,usename列用于存储用户名,密码用于用户的密码。



现在让我们创建一个名为Emplogin的存储过程,如下所示:



Identifying the user that is authentication is a very important task in any application development and I have often read forum posts asking how to create the login page using a Stored Procedure. So by considering the above requirement I have decided to write this article to help beginners, students and who wants to learn how to create a Login page using a Stored Procedure.

So let us start with the walkthrough.

First create one table named Emp_Login in the SQL database with two columns username and password:
create table Emp_Login(userName nvarchar(50), Password nvarchar(50))


Insert the One Record in the table as:
Insert into Emp_Login (userName,Password) values ('vijay','yadav')


In the above table the usename column is used to store the User Name and Password is for the user's Password.

Now let us create a Stored Procedure named Emplogin as follows:

Create  procedure Emplogin
(
@Usename Varchar (20),
@Password varchar (10)
)
as
Begin
Select COUNT(*)from Emp_Login where username=@Usename and password=@Password 
End





在上述存储过程中名为Emplogin的变量@Usename用于用户名,@ Password用于密码。我在开头写的select查询计算了名称与确切的两个变量匹配的表中的用户数;这是来自前端用户输入页面的@Usename和@Password。



现在创建名为Loginapplication的项目,或者您可以指定任何名称:

开始 - 所有程序 - Microsoft Visual Studio 2012。

文件 - 新项目 - C# - 空项目(以避免添加母版页。)

根据需要为网站命名,如Loginapplication或其他名称。指定位置。

然后右键单击解决方案资源管理器 - 添加新项目 - 登录凭证的Default.aspx页面和成功登录后重定向的Welcome.aspx。

在< form>上拖放两个按钮,一个Label和两个textBoxes。默认aspx页面的一部分。

然后< form>默认aspx页面的部分如下所示:



默认page.png



然后加倍 - 点击btn2并在Button_click函数中写下以下代码:





In the above Stored Procedure named Emplogin the variable @Usename is used for the username and @Password is used for the password. The select query that I have written in the beginning counts the number of users from the table whose name matches with the exact two variables; that is @Usename and @Password which comes from the front end user input page.

Now create the project named Loginapplication or you can specify any name as:
"Start" - "All Programs" - "Microsoft Visual Studio 2012".
"File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
Give the web site a name such as Loginapplication or another as you wish and specify the location.
Then right-click on Solution Explorer - "Add New Item" - Default.aspx page for Login Credential and Welcome.aspx for redirecting after successful login.
Drag and drop two buttons, one Label and two textBoxes on the <form> section of the Default aspx page.
Then the <form> section of the Default aspx page looks as in the following:

Default page.png

Then double-click on btn2 and write the following code in the Button_click function as:

 protected void Button2_Click (objectsender, EventArgs e)
 {
     connection();
     query = "Emplogin";   //stored procedure Name
     SqlCommand com = new  SqlCommand(query, con);
     com.CommandType= CommandType.StoredProcedure;
     com.Parameters.AddWithValue("@Usename", TextBox1.Text.ToString());   //for username 
     com.Parameters.AddWithValue("@Password", TextBox2.Text.ToString());  //for password
 
     int usercount = (Int32)com.ExecuteScalar();// for taking single value
 
     if (usercount == 1)  // comparing users from table 
     {
         Response.Redirect("Welcome.aspx");  //for sucsseful login
     }
     else
     {
         con.Close();
         Label1.Text = "Invalid User Name or Password";  //for invalid login
     }
}





前面代码中最重要的部分是If条件部分,我使用了一个整数变量用户计数,用于使用ExecuteScalar计算存储过程中的单个值,如果在表中找到记录它返回1然后页面被重定向到欢迎页面,否则它给出无效用户名或密码。



现在运行应用程序。



我们的用户名是-vijay

和密码是 - yadav



我们已经在创建表时输入了表格。



The most important part of the preceding code is the If condition part, I have used the one integer variable user count which is used to count the single value from the Stored Procedure using ExecuteScalar, if the records are found in the table it returns 1 then the page is redirected to the Welcome page otherwise it gives Invalid user Name or Password.

Now run the application.

our userName is- vijay
and Password is - yadav

Which we are already entered into the table at the creation of the table.


你应该探索角色和会员资格。



1. 了解ASP.NET角色和成员资格 - 初学者教程 [ ^ ]

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

3. 基于角色的安全性与表单身份验证 [ ^ ]

4. 基于角色的授权(C#) [ ^ ]
You should explore Roles and Memberships.

1. Understanding ASP.NET Roles and Membership - A Beginner's Tutorial[^]
2. Authenticate User by Roles in ASP.NET[^]
3. Role-based Security with Forms Authentication[^]
4. Role-Based Authorization (C#)[^]


这篇关于如何使用存储过程在asp.net c#中创建登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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