首页 > 代码库 > Asp.Net_单点登录

Asp.Net_单点登录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["UsName"] != null)
        {
            Response.Write("记录的用户名:" + Application["UsName"]);
        }
    }
    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void OnloginbtnClick(object sender, EventArgs e)
    {
        if (txt_UsName.Text.Length > 0 && txt_UsPwd.Text.Length > 0)
        {
            if (Application["UsName"] != null)
            {
                //判断用户是否已经登录过
                string[] UsNameArray=Application["UsName"].ToString().Split(‘,‘);
                for (int i = 0; i < UsNameArray.Length; i++)
                {
                    if (UsNameArray[i] == txt_UsName.Text.Trim())
                    {
                        Response.Write("<script>alert(‘你已经登录!‘);</script>");
                        return;
                    }
                }
            }
                string strSplit = "";
                Session["loginUsName"] = txt_UsName.Text.Trim();
                Application.Lock();
                if (Application["UsName"] != null)
                {
                    strSplit = ",";
                }
                Application["UsName"] = Application["UsName"] + strSplit + Session["loginUsName"].ToString();
                Application.UnLock();

                Response.Write("<script>alert(‘成功登录!‘);</script>");
        }
        else
        {
            Response.Write("<script>alert(‘请填写用户名和密码!‘);</script>");
        }
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void OnSignOutBtnClick(object sender, EventArgs e)
    {
        if (Session["loginUsName"] != null)
        {
            Application.Lock();
            Application["UsName"] = Application["UsName"].ToString().Replace(Session["loginUsName"].ToString(), "");
            Application.UnLock();
            Response.Write("<script>alert(‘" + Session["loginUsName"] + "已经成功退出" + "!‘);</script>");
        }
    }
}

    void Application_Start(object sender, EventArgs e) 

    {
        //在应用程序启动时运行的代码
        Application["UsName"] = null;
         
    }

  

Asp.Net_单点登录