2017-09-05 35 views
2

我有一個.NET Core 2 Web應用程序,我想使用ASP.NET身份驗證我的用戶。在.NET Core 1.x上,我的代碼工作正常。AspNet核心身份 - Cookie未在生產中設置

我遷移到.NET Core 2,並且在Visual Studio中本地運行時身份驗證可以正常工作。但是,當我部署到實時環境時,身份驗證停止工作:身份驗證Cookie未在生產中設置。

我Startup.cs代碼如下所示:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddIdentity<AppUser, RavenDB.IdentityRole>() 
     .AddDefaultTokenProviders(); 

    ... 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    ... 

    app.UseAuthentication(); 
} 

要登錄,我的代碼看起來是這樣的:

public async Task<ActionResult> SignIn(...) 
{ 
    var user = ...; // Load the User from the database. 
    await this.signInManager.SignInAsync(user, isPersistent: true); 

    ... 
} 

此代碼本地工作:ASP.NET的身份身份驗證的cookie已設置。但是,當我將其部署到Azure的生產環境中時,cookie永遠不會被設置。

我錯過了什麼?

回答

3

我解決了這個問題。它歸結爲HTTPS:似乎signInManager.SignInAsync(...)設置了一個僅HTTPS的cookie。我最初發布到非HTTPS網站進行測試。

當我發佈到HTTPS站點後,該cookie再次開始工作。

它在本地工作的原因是我在本地運行HTTPS。