我有一个标准的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为空字符串.
我究竟做错了什么?我以同样的方式完成了另一个项目,过去使用相同的设置,我没有遇到任何问题.
web.config中
<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>
任何人都可以建议我做错了什么?它现在正在引发重大问题.
干杯!
解决方法
检查项目中的App_Start文件夹中是否有Startup.Auth.cs文件.
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 vs. SignInManager and cookie expiration