天天看點

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

繼續閱讀