.net 身份会话启动
在MVC5项目中,我使用的是Microsoft.AspNet.Identity。我想授权用户使用cookie和会话。我在redis上录制会话.net 身份会话启动,.net,session,cookies,asp.net-identity,.net,Session,Cookies,Asp.net Identity,在MVC5项目中,我使用的是Microsoft.AspNet.Identity。我想授权用户使用cookie和会话。我在redis上录制会话 <sessionState mode="Custom" customProvider="MySessionStateStore"> <providers> <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSe
<sessionState mode="Custom" customProvider="MySessionStateStore">
<providers>
<add name="MySessionStateStore"
type="Microsoft.Web.Redis.RedisSessionStateProvider"
host="192.168.13.197"
port = "6379"
accessKey = ""
ssl = "false"
throwOnError = "true"
retryTimeoutInMilliseconds = "5000"
databaseId = "0"
applicationName = "IddaaWebSite"
connectionTimeoutInMilliseconds = "5000"
operationTimeoutInMilliseconds = "1000"/>
</providers>
</sessionState>
我想我需要验证一下这个方法。它应该检查redis上的cookie和会话?Identity framework不依赖会话来存储任何身份验证数据,所以您必须自己实现 我认为与Redis会话相关的cookie失效的最佳位置是
OnValidateIdentity
事件。它在Startup.Auth.cs
中可用,如下所示(默认模板):
公共部分类启动
{
public void ConfigureAuth(IAppBuilder应用程序)
{
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
//配置登录cookie
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
//其他东西
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity——这只是一个示例,说明如何在那里实现自己的逻辑
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
if (user.EmailConfirmed == false)
return View("_ActivationCodeManuel", user);
await SignInAsync(user, model.RememberMe);
var uSo = JsonConvert.SerializeObject(user);
Session.Add(user.Id, uSo);
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError("", "E-posta adresinizi ya da şifrenizi hatalı girdiniz.");
}
// If we got this far, something failed, redisplay form
return View(model);
}
[Authorize]
public ActionResult Index()
{
var id = User.Identity.GetUserId();
return View();
}
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// 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
{
// other stuff
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
// other stuff