【发布时间】:2017-08-25 11:24:43
【问题描述】:
我有一个 ASP.Net Core 2 项目,我设置了所有身份验证配置
在 ASP.Net Core 2 Preview2\Start.cs 中
var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"C:\SharedFolder"));
var dataProtector = protectionProvider.CreateProtector(
"CookieAuthenticationMiddleware",
"Cookie",
"v2");
var ticketFormat = new TicketDataFormat(dataProtector);
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(o =>{
o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
services.ConfigureApplicationCookie(c => {
c.CookieName = "myapplication";
c.ExpireTimeSpan = TimeSpan.FromDays(3);
c.CookieSecure = CookieSecurePolicy.None;
c.SlidingExpiration = true;
});
在ASP.Net MVC4\ConfigureAuth函数中:
var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"C:\SharedFolder"));
var dataProtector = protectionProvider.CreateProtector(
"CookieAuthenticationMiddleware",
"Cookie",
"v2");
var ticketFormat = new AspNetTicketDataFormat(new DataProtectorShim(dataProtector));
app.UseCookieAuthentication(new CookieAuthenticationOptions{
AuthenticationType = "Cookie",
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
CookieName = "myapplication",
CookieDomain = "localhost",
TicketDataFormat = ticketFormat,
CookieManager = new ChunkingCookieManager()
});
share Cookies 工作正常我在 Asp.Net Core2 项目中设置了 cookie,我从 MVC 项目中读取它,反之亦然,当我检查用户 IsAuthenticated 时,在 MVC 项目中总是得到错误。
如何在 MVC 项目中获取用户身份?任何帮助都非常受欢迎,在此先感谢。
【问题讨论】:
标签: asp.net asp.net-mvc-4 cookies .net-core