MVC5身份/ OWIN - Signout事件 [英] MVC5 Identity/OWIN - Signout events

查看:83
本文介绍了MVC5身份/ OWIN - Signout事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测所有可能的SignOuts?有没有办法得到一些事件,当SignOut手工制作,由超时和其他任何可能的方式?

How to detect all possible SignOuts? Is there a way to get some event when SignOut is made manually, by timeout and any other possible ways?

我需要知道什么时候用户认证结束像我知道在启动时唱歌被调用。

I need to know when user authentication ends like i know that it starts when SingIn is called.

我同时使用内部帐户和外部(如Facebook)。

I'm using both internal accounts and external (like Facebook).

推荐答案

我需要一个点,当用户已经通过身份验证,我可以初始化用户会话。下面是code,它应该做的工作,我不喜欢它。我需要在每个请求检查会议,以确保在会议标志已经被加载并加载当前用户。

I need a spot that i can initialize user session when user is already authenticated. Below is code that should do the job, i don't like it at all. I need to check session at each request to be sure that flag in session is already loaded and was loaded for current user.

在未来的标志很可能是一整类属性从数据库。

In future that flag will be probably a whole class of properties from DB.

无法相信有检测没有更好的方法,当用户SignedIn /输出。

Can't believe that there is no better way of detecting when user is SignedIn/Out.

我所做的是:

void Session_Start(object sender, EventArgs e)
{
    InitializeSession_Authenticated();
}

protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
    InitializeSession_Authenticated();
}

private void InitializeSession_Authenticated()
{
    if (HttpContext.Current.Session == null)
        return;
    if (!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
        return;

    string userName = System.Threading.Thread.CurrentPrincipal.Identity.Name;
    if (HttpContext.Current.Session[SESSION_VAR.USER_ISINITIALIZED] == null || 
        HttpContext.Current.Session[SESSION_VAR.USER_NAMEFORSESSION] != userName)
    {
        HttpContext.Current.Session[SESSION_VAR.USER_NAMEFORSESSION] = userName;
        HttpContext.Current.Session[SESSION_VAR.USER_ISINITIALIZED] = GetFlagFromDB(userName);
    }
}

编辑:@Shoe感谢:P ...

@Shoe thanks :P...

这篇关于MVC5身份/ OWIN - Signout事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆