Asp.net-Mvc

在 PasswordSignInAsync 成功後,User.Identity.IsAuthenticated 始終為假

  • February 10, 2018

我有一個標準的 MVC 項目,其中包含 UserManager 和 SignInManager 對像以及 AccountController,以及預先創建的登錄和註冊類型功能。

我可以在我的 AspNetUsers 表中註冊新使用者,但是當我登錄時,我會呼叫:-

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

數據正確地來自表單,結果是我預期的成功。

然後我嘗試了以下重定向:-

case SignInStatus.Success:
   //return RedirectToLocal("/admin/");
   return RedirectToAction("Index", "Admin");

但在任何頁面上,成功登錄後,User.Identity.IsAuthenticated 始終為 false,而 User.Identity.Name 為空字元串。

我究竟做錯了什麼?我過去以相同的方式使用相同的設置完成了另一個項目,並且我遇到了零問題。

網路配置

<system.web>
   <compilation debug="true" targetFramework="4.5.1" />
   <httpRuntime targetFramework="4.5.1" />
   <!--<authentication mode="Forms">
       <forms loginUrl="~/Account/Login/" timeout="1000" />
   </authentication>-->
   <authentication mode="None" />
</system.web>
<modules>
   <remove name="FormsAuthentication" />
</modules>

誰能建議我做錯了什麼?它現在正在引起重大問題。

乾杯!

檢查項目的文件夾中是否有Startup.Auth.cs文件。App_Start

public partial class Startup {
   public void ConfigureAuth(IAppBuilder app) {            
       // This uses cookie to store information for the signed in user
       var authOptions = new CookieAuthenticationOptions {
           AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,                
           LoginPath = new PathString("/Account/Login"),
           LogoutPath = new PathString("/Account/Logout"),
           ExpireTimeSpan = TimeSpan.FromDays(7),
       };
       app.UseCookieAuthentication(authOptions);
   }
}

並從Startup類中呼叫

public partial class Startup {
   public void Configuration(IAppBuilder app) {
       // Surface Identity provider
       ConfigureAuth(app);

       //..other start up code
   }
}

根據您使用的 asp.net 版本和身份,您應該看看這個

ASP.NET Identity AuthenticationManager 與 SignInManager 和 cookie 過期

引用自:https://stackoverflow.com/questions/36366059