Instantly shut down or set log out for users
I have AS开发者_运维百科P .NET MVC 2.0 WebApplication, and i need to have a button named "Shut down" or "Block all". It should log out all users, or something like this (mb shut down server).
Is it possible? Thx.
If you are using Forms Authentication unless you track your users with some Session it would be impossible to log users out. Authenticated users are tracked by cookies and even if you restart the application domain using the HttpRuntime.UnloadAppDomain();
method client browsers still have the authentication cookies and users will be automatically logged in on subsequent requests. But if you store some user specific information in the session when you restart the application domain this information would be deleted and you would know that the user should no longer be authenticated as it has no associated session data.
One possibility is to set a flag somewhere that indicates this has happened. Then have an action filter on all of your controllers that checks for this flag. If it is set, log the user out (this depends on what sort of authentication you are using) and optionally redirect them to a page that explains what is going on.
Ok, in the end i decided to make 2 ways in one solution. At first, it should create App_offline.htm (just for beautifull) page in wwwroot directory and block all users. When page would be removed, 2 ways:
- Cookie expired
- Opened web page, but after first click, user would be redirected to /Account/LogOn
After administrator unblock users, they could login. Don't know what about other, for me its not so bad issue. Thx all.
public class LogActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (Membership.GetUser(HttpContext.Current.User.Identity.Name).IsLockedOut)
{
IFormsAuthenticationService FormsService = new FormsAuthenticationService();
FormsService.SignOut();
}
// The action filter logic.
}
}
In controller
public ActionResult ShutDown()
{
StreamWriter sw = new StreamWriter("C:\\inetpub\\wwwroot\\iMagazin\\App_offline.htm");
sw.Close();
_dataManager.Users.BlockAll();
return RedirectToAction("LogOn", "Account");
}
Apply ActionFilter to Controller
[SUPNew.Models.Extansions.LogActionFilter]
public class SuperadministratorController : Controller
{
//...
}
精彩评论