Why ASP.NET Identity returns 401 instead redirect to login page?
我正在尝试使用代码优先功能将ASP NET身份添加到现有的mvc5-项目中。
我研究了生成VS的新项目,并且阅读了许多有关ASP.NET Identity的文章。
在大多数情况下,我将代码从模板复制到了我的项目中。我只是在IdentityDbContext上替换了DBContext,几乎没有更改。表是由EF迁移生成的,并通过UserManager和RoleManager通过Seed方法填充,因此,我认为某些代码可以按预期工作。
但是,当我将[Authorize]添加到Controller(不是WebApi,仅是Controller),并尝试从控制器获取页面时,我遇到了一个问题-服务器返回401.0错误,尽管选项设置为
这是我的代码Startup.Auth.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | namespace App { public partial class Startup { public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(CwDb.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } }); } } } |
这是我的Startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 | [assembly: OwinStartupAttribute(typeof(CarWashApplication.Startup))] namespace App { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } } } |
我只需要带有登录/密码和cookie的简单身份验证,不需要社交网络等身份验证。
我在哪里错了?如何像在vs生成的模板中那样获得重定向?
我找到了解决方案。我只是将