欢迎光临
我们一直在努力

ASP.net Logion用户登陆验证代码-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

web.config

<configuration>

……

<system.web>

       <database>

        <add key=”strconn” value=”server=107.120.65.99;database=db;uid=db;pwd=db” />

</database>

</system.web>

……

</configuration>

 

global.asax.cs

protected void session_end(object sender, eventargs e)

{

        application.lock();

        application.remove(session.sessionid.tostring());

        application[“ol_usercount”]=(int)application[“ol_usercount”]-1;

        application.unlock();

}

 

login.aspx

 

<table class=”logintable”>

<tr>    <td colspan=”2″ class=”tdcaption”><font class=”fcaption”>ログイン</font></td></tr>

<tr>        <td><font class=”font100″>ユーザーid:</font></td></tr>

        <td><asp:textbox cssclass=”itxt” id=”txtname” runat=”server”></asp:textbox></td></tr>

<tr>        <td><font class=”font100″>パスワード:</font></td>

        <td><asp:textbox cssclass=”itxt” id=”txtpwd” textmode=”password” runat=”server”></asp:textbox></td></tr>

<tr>        <td><asp:button cssclass=”ibutton” id=”btnsubmit” text=”login” runat=”server”></asp:button></td>

        <td><input type=”reset” class=”ibutton” name=”btnreset” value=”reset”></td></tr>

</table>

 

login.aspx.cs

using system.collections.specialized;

using system.data.sqlclient;

 

public void loginbysql()

{

     string strconn=(string) ((namevaluecollection) context.getconfig(“system.web/database”))[“strconn”];

     using (sqlconnection conn = new sqlconnection(strconn))

     {

         sqlcommand cmd = new sqlcommand(“sp_idpwd”,conn);

         cmd.commandtype=commandtype.storedprocedure;    //这里采用存储过程

         cmd.parameters.add(“@id”,sqldbtype.varchar,20);

         cmd.parameters.add(“@pwd”,sqldbtype.varchar,20);

         cmd.parameters[“@id”].value=txtname.text;

         cmd.parameters[“@pwd”].value=txtpwd.text;

         conn.open();

 

         using (sqldatareader dr = cmd.executereader())

         {

             if(dr.read())

             {

                bool notol=true;

                for(int i=0;i<application.count;i++)

                {

                   if(application[application.getkey(i).tostring()].tostring()==dr[“userid”].tostring())

                   notol=false;

                }

                if(notol)

                {

                   application[session.sessionid.tostring()]=dr[“userid”].tostring();

                   application[“ol_usercount”]=(int)application[“ol_usercount”]+1;

                   session[“username”]=dr[“username”].tostring();//sessions
                   response.redirect(“./main/main.aspx”);

                }

                else

                   message.text=”已经登陆”;

             }

             else

             {

                message.text=”密码错误”;

             }

        }

    }

}

存储过程
alter procedure dbo.sp_idpwd
 (
  @id varchar(20),
  @pwd varchar(20)
  /*@pwd datatype output*/
 )
as
 set nocount on
 select * from [db].[db].[admin] where (userid=@id) and ([password]=@pwd)
 return

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.net Logion用户登陆验证代码-.NET教程,Asp.Net开发
分享到: 更多 (0)