作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当用户登录 ASP.net 网站时,有没有办法收到通知?
Note: A user can become logged in without visiting a "login page". If the "remember me" cookie exists, they can hit an arbitrary page and be logged in.
Note: There is the
Login.LoggedIn
event. Problem is that that control doesn't exist on every page; and the one page it is present on (Login.aspx
) doesn't callOnLoggedIn
event.
Global.asax
有一个全局的 On Session Start 通知:
void Session_Start(object sender, EventArgs e)
{
}
void LoggedIn(object sender, EventArgs e)
{
}
最佳答案
我认为你没有一个独特的地方来做到这一点。在我的情况下(MVC + log4net)我使用这个:
Global.asax
我使用预先存在的 cookie 检查经过身份验证的用户。protected void Session_Start()
{
string ip = HttpContext.Current.Request.UserHostAddress;
log.InfoFormat("Starting session: {0} from {1}.",Session.SessionID, ip);
if ((HttpContext.Current != null) &&
(HttpContext.Current.User != null) &&
(HttpContext.Current.User.Identity.IsAuthenticated) )
{
string user = HttpContext.Current.User.Identity.Name;
string type = "Cookie";
log.InfoFormat("User {0} logged in with {1}.", user, type);
}
}
Login.OnLoggedIn
中执行此操作)[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.EMail, model.Password, persistCookie: model.RememberMe))
{
string user = model.EMail;
string type = "Forms";
log.InfoFormat("User {0} logged in with {1}.", user, type);
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
log.ErrorFormat("Bad password or user name. User={0}", model.EMail, model.Password);
return View(model);
}
[AllowAnonymous]
public ActionResult ExternalLoginCallback(string returnUrl)
{
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
if (!result.IsSuccessful)
{
log.Debug("External login failure.");
return RedirectToAction("ExternalLoginFailure");
}
if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{
log.InfoFormat("User {0} logged in with External {1} login. External UserID = {2}",
Membership.GetUser(OAuthWebSecurity.GetUserName(result.Provider, result.ProviderUserId)).UserName,
result.Provider,
result.ProviderUserId);
return RedirectToLocal(returnUrl);
}
...
}
关于asp.net - ASP.net 中的全局 OnLoggedIn 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002245/
当用户登录 ASP.net 网站时,有没有办法收到通知? Note: A user can become logged in without visiting a "login page". If t
我有一个带有 onloggedin 事件处理程序的 .Net 登录控件。 onloggedin="Login2_LoggedIn" 但是 User.Identity 始终为 null。 protec
我是一名优秀的程序员,十分优秀!