2017-08-12 135 views
0

我有一個ASP.Net的Core 2項目,我設置的所有驗證配置ASP.NET MVC應用程序標識不與ASP.NET核心共享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() 
      }); 

份額餅乾可以正常運行我Asp.Net的Core2項目設置的cookie和我讀t形式MVC項目和反之亦然正確,當我檢查用戶IsAuthenticated總是在MVC項目中得到錯誤。

我如何在MVC項目中獲得用戶身份?任何幫助都比歡迎,提前謝謝。

回答

0

我通過承諾在ASP.Net核心的幾行2 Preview2 \ Start.cs中

/*services.AddAuthentication(o =>{ 
         o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
         o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
         o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;      
        });*/ 

解決了我的問題,我改變ConfigureApplicationCookie到

services.ConfigureApplicationCookie(c => 
      { 
       c.CookieName = "myapplication"; 
       c.ExpireTimeSpan = TimeSpan.FromDays(3); 
       c.SlidingExpiration = true; 
       c.CookieDomain = "localhost"; 
       c.TicketDataFormat = ticketFormat; 
      });