4
I have a web system developed in ASP.NET MVC 4.
One of the features is user management. A CRUD
of users.
My method of login
is as follows:
[HttpPost]
public ActionResult Login(LoginViewModel loginViewModel)
{
if (_loginService == null)
_loginService = new LoginService();
var result = _loginService.Login(loginViewModel.User, loginViewModel.Password);
if (!result.Error)
{
var userData = JsonConvert.SerializeObject(result.User);
FormsAuthentication.SetAuthCookie(result.User.Id, false);
var ticket = new FormsAuthenticationTicket(1, result.Id, DateTime.Now, DateTime.Now.AddMinutes(9999), true, userData, FormsAuthentication.FormsCookiePath);
var encryptedCookie = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie) { Expires = DateTime.Now.AddHours(14) };
Response.Cookies.Add(cookie);
}
return new JsonResult
{
Data = result
};
}
Yes, it is in English because the system will be maintained by several companies.
Anyway, I treat the return of this method on the client side, with javascript.
As you can imagine, I use the attribute [Authorize]
throughout Controller
where authentication is mandatory.
Let’s assume I just logged into the system with the user StackOverflow
. I am browsing normally until another user identified as DoMal
resolves to delete me from the system. As I am only deleting the user in the action of Delete
, the user StackOverflow
will normally browse the site even when it is deleted. Until, of course, the cookie expires. The problem is I want some way to end his session right away.
Do you have any way to end the session StackOverflow
in IIS? Or force the cookie to expire?
The only thing I don’t want to do is create an online user existential check on every action taken on the site.
Any ideas, suggestions?
Here’s a start from where you can go. http://stackoverflow.com/questions/12379215/how-to-force-logout-user-when-his-her-username-is-changed-by-another-user
– Marconi
When the user is removed, you have access to his id (or something like that). What prevents you from calling Signout on that user? In this case you would "drop" this user only in the controller method that removes such a user. To handle the cookie, have you tried Formsauthentication.Setauthcookie(user, false)? If nothing works, Voce can do your checking in Application_beginrequest instead of spreading through the system.
– victor
@Video: Why the
SignOut
does not accept parameters.. I want to do theSignOut
of a user other than the current. Yes, I already use theFormsAuthentication.SetAuthCookie(user, false)
. Regarding the implementation ofbeginRequest
is just what I don’t want to do: At allrequest
, validate the user’s existence in the database.– Marllon Nasser
@Marllonnasser please, if the answer below is correct, please sign. thank you
– Thomas Erich Pimentel
@Thomaserichpimentel: The answer does not meet my specific need but would work in general.
– Marllon Nasser