ASP.NET Identity 2在cookie身份验证后执行代码
我正在通过OWIN middlewear使用ASP.NET Identity 2身份验证。我已经使用该模板创建了一个新项目,因此最初使用默认生成的代码开始,但对其进行了一些更改(去掉了实体框架,并连接到我自己现有的身份验证中)。这一切都在起作用 我现在想做的是在用户通过保存的cookie登录后执行代码。我已经查看了Startup.Auth.cs文件中的ConfigureAuth,我将其配置如下:ASP.NET Identity 2在cookie身份验证后执行代码,asp.net,asp.net-mvc,asp.net-identity,Asp.net,Asp.net Mvc,Asp.net Identity,我正在通过OWIN middlewear使用ASP.NET Identity 2身份验证。我已经使用该模板创建了一个新项目,因此最初使用默认生成的代码开始,但对其进行了一些更改(去掉了实体框架,并连接到我自己现有的身份验证中)。这一切都在起作用 我现在想做的是在用户通过保存的cookie登录后执行代码。我已经查看了Startup.Auth.cs文件中的ConfigureAuth,我将其配置如下: public void ConfigureAuth(IAppBuilder app) {
public void ConfigureAuth(IAppBuilder app) {
// Configure the user manager and signin manager to use a single instance
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider {
OnResponseSignIn = ctx => {
Log.Trace("On Response Sign In.");
},
OnResponseSignedIn = ctx => {
Log.Trace("On Response Signed In.");
},
OnValidateIdentity = async ctx => {
Log.Trace("On Validate Identity.");
}
}
});
}
public void ConfigureAuth(IAppBuilder应用程序){
//将用户管理器和登录管理器配置为使用单个实例
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationSignInManager.Create);
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
//配置登录cookie
app.UseCookieAuthentication(新的CookieAuthenticationOptions{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider{
OnResponseSignIn=ctx=>{
Log.Trace(“在响应登录时”);
},
OnResponseSignedIn=ctx=>{
Log.Trace(“在响应登录时”);
},
OnValidateIdentity=async ctx=>{
Log.Trace(“在验证身份时”);
}
}
});
}
由此我可以看出,只有在用户输入用户名和密码的实际登录过程中,才会点击OnResponseSignIn和OnResponseSignedIn。当用户通过保存的cookie进行身份验证时,它们不会被命中
无论用户是通过用户名/密码进行身份验证还是通过保存的cookie进行身份验证,都会命中OnValidateIdentity,并且每次请求都会命中OnValidateIdentity
我希望在通过cookie登录后只执行一次代码。有人知道怎么做吗?如果不是,我想另一个选择是将代码放在OnValidateIdentity中,但放在If语句中,除非它是cookie身份验证后的第一个调用,否则将阻止它运行。有人能想到如何实现这一点吗?我所能想到的就是在代码第一次运行后在会话中设置一个变量,并检查它是否存在以防止重新运行?可能可以通过使用会话变量作为标志来完成,并且只在未设置时执行操作
OnValidateIdentity = async context => {
if (HttpContext.Current.Session["Refreshed"] == null)
{
/** do your thing **/
...
HttpContext.Current.Session["Refreshed"] = new object();
}
}
任务的问题是“通过cookie登录后只执行一次代码”。什么是“通过cookie登录”?我想你可能想知道用户是否有一个持久的cookie,并在不活动的情况下返回到你的站点。但什么是“不活动”?5分钟足够不活动吗?24小时怎么样?48小时?一旦你定义了这个,我可能会告诉你去哪里看。@trailmax谢谢你,并为这次耽搁道歉。是的,你一针见血:这是因为他们有一块持久的饼干。基本上,如果他们在没有cookie的情况下访问站点,他们输入用户名和密码,我就可以进入这个过程。问题是当他们带着一个持久的cookie回来时。本质上,我希望代码在他们返回并启动新会话时运行,而不管它已经运行了多长时间。也就是说,如果他们回来,提供cookie,应用程序允许他们进入,避免输入用户名和密码,我希望运行一些代码。再次感谢@对不起,我想我没有。