Asp.net登录问题。 [英] Asp.net Login problem.

查看:84
本文介绍了Asp.net登录问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,



i有网站但没有母版页。



i有代码每一页。



如上所述我也有代码fopre登录页面。



但是当我转到我项目中的另一个页面登录页面的会话是expire.it'自动注销。





我能做什么??





代码如下:



使用系统; 
使用System.Collections;
使用System.Configuration;
使用System.Data;
使用System.Linq;
使用System.Web;使用System.Web.Security
;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;
使用System.Net.Mail;
使用System.Data.SqlClient;
使用System.IO;
使用System.IO.Compression;

公共部分类登录:System.Web.UI.Page
{public string GetConnectionString()
{
//设置Web配置文件中的连接字符串ConnString是连接字符串的名称
返回System.Configuration.ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString;
}
protected void Page_Load(object sender,EventArgs e)
{



}
protected void LoginButton_Click(object sender,ImageClickEventArgs e)
{
string sql =stp_GetUserType;
try
{


SqlConnection conn = new SqlConnection(GetConnectionString());

conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ UserName,UserName.Text);
cmd.Parameters.AddWithValue(@ Password,Zip(Password.Text));

SqlDataReader objReader = cmd.ExecuteReader();
if(objReader.Read())
{
int nResult = Convert.ToInt32(objReader [0]);
if(nResult == 1)
{
// Admin
Div1.Visible = false;
DivLogOff.Visible = true;
lblLoginUserName.Text = UserName.Text;
Session [IsAdmin] = true;
Session [LoginUser] = UserName.Text;


}
否则if(nResult == 0)
{
//正常
Div1.Visible = false;
DivLogOff.Visible = true;
lblLoginUserName.Text = UserName.Text;
Session [IsAdmin] = false;
Session [LoginUser] = UserName.Text;


}
else
{
//无效用户
Div1.Visible = true;
DivLogOff.Visible = false;

会话[IsAdmin] = false;

}

}
else
{
}
if(Page is IPage)
{
((IPage)Page)。Login();
}
conn.Close();
}
catch(SqlException ex)
{

}
{
string a;
a = lblCurrentlyUserText.Text;
Session [Item] = a;
}
}

protected void LogoffButton_Click(object sender,ImageClickEventArgs e)
{
{
Session [LoginUser] = null ;

DivLogOff.Visible = false;
Div1.Visible = true;
if(Page is IPage)
{
((IPage)Page)。Logof();
}
}
}

public byte [] Zip(字符串值)
{
//将字符串转换为byte []
byte [] byteArray = new byte [value.Length];
int indexBA = 0;
foreach(value.ToCharArray()中的char项)
{
byteArray [indexBA ++] =(byte)item;
}

//准备压缩
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Compress);

//压缩
sw.Write(byteArray,0,byteArray.Length);
//关闭,不要FLUSH导致字节丢失...
sw.Close();

//将byte [] zip数据转换为字符串
byteArray = ms.ToArray();
//System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
// foreach(byteArray中的字节项)
// {
// sB.Append((char)item);
//}
//ms.Close();
//sw.Dispose();
//ms.Dispose();
//返回sB.ToString();
返回byteArray;
}

public static string UnZip(string value)
{
//将字符串转换为byte []
byte [] byteArray = new byte [ value.Length];
int indexBA = 0;
foreach(value.ToCharArray()中的char项)
{
byteArray [indexBA ++] =(byte)item;
}

//准备解压缩
System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Decompress);

//重置变量以收集未压缩的结果
byteArray = new byte [byteArray.Length];

//解压缩
int rByte = sr.Read(byteArray,0,byteArray.Length);

//将byte []解压缩数据转换为字符串
System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
//读取GZipStream红色的字节数,不要为
// resultByteArray中的每个字节a;
for(int i = 0; i< rByte; i ++)
{
sB.Append((char)byteArray [i]);
}
sr.Close();
ms.Close();
sr.Dispose();
ms.Dispose();
返回sB.ToString();
}
}

解决方案

请参阅我最近回答的问题此处

Dear Sir,

i Have website but does not having Master page.

i have code for each and every page .

as above i have also code fopre login page .

but when i go to another page in my project the session of login page is expire.it''s automatically logout .


What can i do??


code is as follows :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;

public partial class Login : System.Web.UI.Page
{public string GetConnectionString()
    {
        //sets the connection string from your web config file "ConnString" is the name of your Connection String
        return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
          


    }
    protected void LoginButton_Click(object sender, ImageClickEventArgs e)
    {
        string sql = "stp_GetUserType";
        try
        {


            SqlConnection conn = new SqlConnection(GetConnectionString());

            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", UserName.Text);
            cmd.Parameters.AddWithValue("@Password", Zip(Password.Text));

            SqlDataReader objReader = cmd.ExecuteReader();
            if (objReader.Read())
            {
                int nResult = Convert.ToInt32(objReader[0]);
                if (nResult == 1)
                {
                    //Admin
                    Div1.Visible = false;
                    DivLogOff.Visible = true;
                    lblLoginUserName.Text = UserName.Text;
                    Session["IsAdmin"] = true;
                    Session["LoginUser"] = UserName.Text;


                }
                else if (nResult == 0)
                {
                    //Normal
                    Div1.Visible = false;
                    DivLogOff.Visible = true;
                    lblLoginUserName.Text = UserName.Text;
                    Session["IsAdmin"] = false;
                    Session["LoginUser"] = UserName.Text;


                }
                else
                {
                    //Invalid User
                    Div1.Visible = true;
                    DivLogOff.Visible = false;

                    Session["IsAdmin"] = false;

                }

            }
            else
            {
            }
            if (Page is IPage)
            {
                ((IPage)Page).Login();
            }
            conn.Close();
        }
        catch (SqlException ex)
        {

        }
        {
            string a;
            a = lblCurrentlyUserText.Text;
            Session["Item"] = a;
        }
    }

    protected void  LogoffButton_Click(object sender, ImageClickEventArgs e)
{
        {
        Session["LoginUser"] = null;

        DivLogOff.Visible = false;
        Div1.Visible = true;
        if (Page is IPage)
        {
            ((IPage)Page).Logof();
        }
    }
}

public byte[] Zip(string value)
    {
        //Transform string into byte[]  
        byte[] byteArray = new byte[value.Length];
        int indexBA = 0;
        foreach (char item in value.ToCharArray())
        {
            byteArray[indexBA++] = (byte)item;
        }

        //Prepare for compress
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
            System.IO.Compression.CompressionMode.Compress);

        //Compress
        sw.Write(byteArray, 0, byteArray.Length);
        //Close, DO NOT FLUSH cause bytes will go missing...
        sw.Close();

        //Transform byte[] zip data to string
        byteArray = ms.ToArray();
        //System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
        //foreach (byte item in byteArray)
        //{
        //    sB.Append((char)item);
        //}
        //ms.Close();
        //sw.Dispose();
        //ms.Dispose();
        //return sB.ToString();
        return byteArray;
    }

    public static string UnZip(string value)
    {
        //Transform string into byte[]
        byte[] byteArray = new byte[value.Length];
        int indexBA = 0;
        foreach (char item in value.ToCharArray())
        {
            byteArray[indexBA++] = (byte)item;
        }

        //Prepare for decompress
        System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
        System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
            System.IO.Compression.CompressionMode.Decompress);

        //Reset variable to collect uncompressed result
        byteArray = new byte[byteArray.Length];

        //Decompress
        int rByte = sr.Read(byteArray, 0, byteArray.Length);

        //Transform byte[] unzip data to string
        System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
        //Read the number of bytes GZipStream red and do not a for each bytes in
        //resultByteArray;
        for (int i = 0; i < rByte; i++)
        {
            sB.Append((char)byteArray[i]);
        }
        sr.Close();
        ms.Close();
        sr.Dispose();
        ms.Dispose();
        return sB.ToString();
    }
}

解决方案

Refer to my recently answered question here


这篇关于Asp.net登录问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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