天天看点

C#登出系统并清除Cookie

C#登出系统并清除Cookie

1.前端页面代码:

前端页面代码主要显示退出系统或者网站的可视化按钮代码,代码如下:(请忽略项目关键字:CPU)

<ul class="nav navbar-nav navbar-right">                        
                            <li class="">
                                <a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                    <img src="@Url.Content("~/assets/img/user2.png")" alt="">@CPU.UserProfile.User.UserID
                                    <span class="fa fa-angle-down"></span>
                                </a>
                                <ul class="dropdown-menu dropdown-usermenu pull-right" style="width:35px">
                                    <li>
                                        @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", style = "padding: 12px 0px;" }))
                                        {
                                            @Html.AntiForgeryToken()
                                            <a href="javascript:document.getElementById('logoutForm').submit()"><i class="fa fa-sign-out pull-right"></i> Log Out</a>
                                        }
                                    </li>
                                </ul>
                            </li>
                            <li class="hidden-xs"><a><span>在线人数: @System.Web.HttpContext.Current.Application["OnLineUserCount"] 人</span></a></li>                            
                        </ul>       

2.后台逻辑处理代码:

后台的代码,主要是对前台点击按钮的事件进行处理:退出系统和清除系统Cookie

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();//退出登录
            System.Web.HttpContext.Current.Session.Abandon();//清理缓存
            return RedirectToAction("Login", "Account");
        }      

3.界面效果:

C#登出系统并清除Cookie

本文来自博客园,作者:農碼一生,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/12132615.html

技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正! 个人开源代码链接: GitHub:

https://github.com/ITMingliang

Gitee:

https://gitee.com/mingliang_it

GitLab:

https://gitlab.com/ITMingliang

进开发学习交流群:
C#登出系统并清除Cookie

继续阅读