C#Mvc退出登录

调用MyWebApp类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Droplets.Models;
using System.Web;

namespace Droplets.WebCode
{
public static class MyWebApp
{
private const string SessionUser = "CurrentUser";
public const string LoginUrl = "/Management/Login";
public const string LogoutUrl = "/Management/Logout";

public static Admin currentUser
{
get
{
if (HttpContext.Current == null) return null;
return HttpContext.Current.Session[SessionUser] as Admin;
}
set
{
HttpContext.Current.Session[SessionUser] = value;
}
}
public static Admin checkLogin()
{
var user = currentUser;
if (user == null)
{
HttpContext.Current.Response.Redirect(LoginUrl);
}
return user;
}
public static void logout()
{
HttpContext.Current.Session.Remove(SessionUser);
}
}
}

Logout.View部分:

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Logout</title>
</head>
<body>
<div>
<h2>你已经退出登录!</h2>
<a href="/Management/Login">重新登录</a>
</div>
</body>
</html>

ManagementControllewr部分:

public class ManagementController : AppController
{
//
// GET: /Management/
Droplets.Models.DropletsEntities db;

public ManagementController()
{
db = new DropletsEntities();
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}

public ActionResult Logout()
{
MyWebApp.logout();
return View();
}

}

原文地址:https://www.cnblogs.com/DotaSteam/p/5448292.html