asp.net 单点登录,浏览器关闭清空session

1.登录验证:

   UsersDO usersDO = new UsersDO();
usersDO.UserName = this.txtUserName.Text.Trim();
 usersDO.Pwd = Tools.EncryptString(this.txtPwd.Text);
UsersDO loginUser= new UsersDAO().GetUsersByLogin(usersDO);
if (loginUser != null)
{
          string cacheKey = Convert.ToString(Cache[loginUser.UserName]);
            // Cache中没有该str_Key的项目,表名用户没有登录,或者已经登录超时
            if (cacheKey == String.Empty)
            {
                // TimeSpan构造函数,用来重载版本的方法,进行判断是否登录。
                TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
                HttpContext.Current.Cache.Insert(loginUser.UserName, loginUser.UserName, null, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, null);
                Session["LoginUser"] = loginUser ;
                // 首次登录成功
                Response.Write("<h2 style='color:red'>你好,登录成功!");
            }
            else
            {
                // 在 Cache 中存在该用户的记录,已经登录过,禁止再次登录
                Response.Write("<h2 style='color:red'>抱歉,您好像已经登录了!");
                return;
            }

        }
        else
        {
            Response.Write("用户名称或密码错误!!!");
        }

2.注销操作
    主页面为非安全登录(如直接关闭浏览器)时,添加js函数处理:
    

    window.onbeforeunload   =   function()   {         var   n   =   window.event.screenX   -   window.screenLeft;         var   b   =   n   >   document.documentElement.scrollWidth-20;         //浏览器关闭和刷新都会执行此方法,下面判断是否关闭       if(b   &&   window.event.clientY   <   0   ||   window.event.altKey)  //关闭浏览器       {             window.location.href="Logout.aspx"; //跳转到安全退出页面         

         //window.event.returnValue   =   "警告这是不安全操作!";     //这里可以放置你想做的操作代码         }   }


    新建layout。aspx页面(主页面安全退出按钮链接此页面),只需在后台添加代码:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginUser"] != null)
        {

HttpContext.Current.Cache.Remove((Session["LoginUser"] as UsersDO).UserName);

                   }

        Session.Clear(); Session.Abandon(); FormsAuthentication.SignOut(); Response.Redirect("../index.aspx"); }

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core中实现单点登录需要进行以下步骤: 1. 配置身份提供者中的客户端信息,包括客户端ID、客户端密钥、回调URL等信息。 2. 在应用中配置身份认证和单点登录,可以使用OpenID Connect等协议来实现。 3. 在需要使用单点登录的Action方法中使用[Authorize]特性进行身份认证。 4. 可以使用ASP.NET Core Identity和IdentityServer4等框架来实现单点注销。 下面是一个简单的示例,演示如何在.NET Core中实现单点登录: ```csharp // Startup.cs public void ConfigureServices(IServiceCollection services) { // 配置身份认证 services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; }) .AddCookie() .AddOpenIdConnect(options => { // 配置OpenID Connect options.Authority = "https://accounts.example.com"; options.ClientId = "client_id"; options.ClientSecret = "client_secret"; options.ResponseType = "code"; options.Scope.Add("openid"); options.Scope.Add("profile"); options.CallbackPath = "/signin-oidc"; options.SaveTokens = true; }); services.AddControllersWithViews(); } // HomeController.cs public class HomeController : Controller { [Authorize] public IActionResult Index() { return View(); } } // LogoutController.cs public class LogoutController : Controller { public async Task<IActionResult> Index() { // 注销当前用户 await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); // 跳转到注销页面 return Redirect("https://accounts.example.com/logout"); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值