如何使用Asp.net中的存储过程重定向到不同的页面c# [英] How to redirect to different pages using stored procedure in Asp.net c#

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

问题描述

亲爱的所有人,



我正在使用Asp.net,C#,SqlServer 2005



In这个项目我有两个面板1)管理员2)用户

在管理员文件夹中我有所有管理页面&在用户文件夹中我有所有用户页面。



现在这里使用商店程序重定向到不同的页面

如果管理员已登录,它必须重定向到管理页面&

如果用户登录,它必须重定向到用户页面。



这是我的代码。 ...







  protected   void  BtnLogin_Click( object  sender,ImageClickEventArgs e)
{
DataTable dt = new DataTable();
try
{
SqlConnection con = new SqlConnection(ConfigurationManager。 ConnectionStrings [ CollegeDataBaseConnectionString]。ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter( CHECK_UserAccounts_Proc ,con);
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
adp.SelectCommand.Parameters.Add( @ USERNAME,SqlDbType.VarChar,< span class =code-digit> 50 )。Value = TxtUserName.Text.Trim();
adp.SelectCommand.Parameters.Add( @ PASSWORD,SqlDbType.VarChar,< span class =code-digit> 50 )。Value = TxtPassword.Text.Trim();
adp.Fill(dt);


if (dt.Rows.Count > < span class =code-digit> 0 )
{

Response.Redirect( 〜/用户/ Home.aspx);
}
else
{
ScriptManager.RegisterStartupScript( this this .GetType(), RunCode javascript:alert('无效的用户名或密码'););
}
}
catch (例外情况)
{
Response.Write( 发生错误: + ex.ToString());
}
最后
{
dt.Clear();
dt.Dispose();
}
}







这是我的存储过程

---------------------------



  CREATE   PROCEDURE  [dbo]。[CHECK_UserAccounts_Proc] 
@ USERNAME VARCHAR 50 ),
@ PASSWORD VARCHAR 50
AS
BEGIN
SELECT * FROM UserInformation WHERE USERNAME COLLATE Latin1_general_CS_AS = @ USERNAME AND [PASSWORD] COLLAT E Latin1_general_CS_AS = @ PASSWORD
END







请帮忙。



谢谢。

解决方案

你必须在'UserInformation'表中维护用户角色。然后您可以检查登录用户的用户角色并根据该值重定向到相关页面。



用户角色列上,您可以拥有管理员,用户等...

第一个建议:不要以纯文本或加密格式直接在数据库中存储密码。



根据答案是有关,我无法看到您尝试重定向管理员的代码。重定向与存储过程无关。好吧,有两个解决方案:一个是使用Roles,为此看到这些链接:



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



了解角色管理 [ ^ ]



另一个是在数据库中有一列作为角色。成功登录后,检查用户在数据库中的角色并相应地重定向。


以下是完整代码的代码段,请尝试如下



  if (dt.Rows.Count >   0 
{
if (dt.Rows [ 0 ]。列[ UserType] == Admin
{
Response.Redirect( 〜/ Admin / Home.aspx);
}
else
{
Response.Redirect( 〜/ User / Home.aspx);
}
}
else
{
ScriptManager.RegisterStartupScript( this this .GetType(), RunCode javascript:alert('无效的用户名或密码'); true );
}


Dear All,

am working on Asp.net , C#, SqlServer 2005

In this project i have two panels 1)Admin 2)User
In Admin Folder I have all admin pages & in user folder i have all user pages.

now here am using store procedure to redirect to different pages
If admin Logged-in, it must redirect to admin pages &
If User Logged-in, it must redirect to user pages.

This is my code....



protected void BtnLogin_Click(object sender, ImageClickEventArgs e)
   {
       DataTable dt = new DataTable();
       try
       {
           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CollegeDataBaseConnectionString"].ConnectionString);
           SqlDataAdapter adp = new SqlDataAdapter("CHECK_UserAccounts_Proc", con);
           adp.SelectCommand.CommandType = CommandType.StoredProcedure;
           adp.SelectCommand.Parameters.Add("@USERNAME", SqlDbType.VarChar, 50).Value = TxtUserName.Text.Trim();
           adp.SelectCommand.Parameters.Add("@PASSWORD", SqlDbType.VarChar, 50).Value = TxtPassword.Text.Trim();
           adp.Fill(dt);


           if (dt.Rows.Count > 0)
           {

               Response.Redirect("~/User/Home.aspx");
           }
           else
           {
       ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Invalid UserName or Password');", true);
           }
       }
       catch (Exception ex)
       {
           Response.Write("Error occured : " + ex.ToString());
       }
       finally
       {
           dt.Clear();
           dt.Dispose();
       }
   }




This is my Stored Procedure
---------------------------

CREATE PROCEDURE [dbo].[CHECK_UserAccounts_Proc]
                        @USERNAME VARCHAR(50),
                        @PASSWORD VARCHAR(50)
AS
BEGIN
 SELECT * FROM UserInformation WHERE USERNAME COLLATE Latin1_general_CS_AS =@USERNAME AND [PASSWORD] COLLATE Latin1_general_CS_AS=@PASSWORD
END




Please can you help.

Thanks.

解决方案

You have to maintain the User Role in your 'UserInformation' table.Then you can check the 'User Role' of the logged-in user and according to that value where redirect it to the relevant page.

On User Role column you can have 'Admin','User',etc...


First suggestion : Don't store password directly in database either in plain text or in encrypted format.

As per as the answer is concerned,i can't see the code where you are trying to redirect the Admins. Redirection has nothing to do with stored procedure.Well,there are two solutions: one is to use Roles,for that see these links :

Authenticate User by Roles in ASP.NET[^]

Understanding Role Management[^]

Another one is to have one column as roles in database. After successful login,check the user's role in database and redirect it accordingly.


Below is snippet code from your full code, please tried like below

if (dt.Rows.Count > 0)
           {
               if (dt.Rows[0].Columns["UserType"] == "Admin")
               {
                    Response.Redirect("~/Admin/Home.aspx");
               }
               else
               {
                    Response.Redirect("~/User/Home.aspx");
               }
           }
           else
           {
       ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Invalid UserName or Password');", true);
           }


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

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