.net – 为什么OWIN的SignInAsync()没有设置IsAuthenticated(或
我有一个.NET,MVC 5,EF 6项目.我们正在连接到外部项目/数据库来验证用户,因此我们需要实现的只是登录和注销方法,而不是普通的CRUD方法.我知道我已经将EF正确映射到外部数据库的表中;我可以看到它得到了用户.
我正在尝试使用OWIN进行身份验证,以便我可以在整个控制器方法中使用[Authorize]等属性.我一直关注this作为我的向导. 在Startup.Auth.cs我有: app.CreatePerOwinContext<CustomUserManager>(CustomUserManager.Create); app.CreatePerOwinContext<CustomSignInManager>(CustomSignInManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,ApplicationUser>( validateInterval: TimeSpan.FromMinutes(30),regenerateIdentity: (manager,user) => user.GenerateUserIdentityAsync(manager) ) },CookieSecure = CookieSecureOption.Always }); 我还有实现接口IUserStore< User>,UserManager< User>和SignInManager< User,string>的类.我的CustomUserStore实现了以下方法,因为源代码说它是如何登录的.我为其他函数(即DeleteAsync(),UpdateAsync()等)实现了虚拟方法,这些方法不需要实现,因为我们对此外部用户db具有只读访问权限… public async Task<User> FindByIdAsync(string id) { using (ExternalDBContext context = new ExternalDBContext()) { return await Task.Run(() => context.Users.Where(u => u.Id == id).ToList().FirstOrDefault() ); } } 然后在我的AccountController.Login()我有: await CustomSignInManager.SignInAsync(model,true,true); return RedirectToAction("Index","Dashboard"); 但是在SignInAsync()之后,User.Identity.IsAuthenticated仍然是false,因此没有任何授权属性不起作用. #1可能的问题:我想知道问题是否与AuthenticationManager有关,SignInManager依赖它.以下是我在Startup.Auth.cs中实现它的方法: public class CustomSignInManager : SignInManager<User,string> { public CustomSignInManager(CustomUserManager userManager,IAuthenticationManager authenticationManager) : base(userManager,authenticationManager) { } public static CustomSignInManager Create(IdentityFactoryOptions<CustomSignInManager> options,IOwinContext context) { return new CustomSignInManager(context.GetUserManager<CustomUserManager>(),context.Authentication); } } 它从上下文中获取Microsoft.Owin.Security.AuthenticationManager. 那么:我是否需要做其他事情来实现身份验证? #2可能的问题:我注意到我的CustomUserManager有一个属性SupportsUserLogin = false. That property需要实现IUserLoginStore.我不想实现另一个无用的界面,其中包含我不使用的方法! 那么:如果SupportsUserLogin = false,这有关系吗?我需要实现该接口还是有办法解决这个问题? 总结:总的来说,对于一个我想要做的就是登录用户的项目来说,这已经太复杂了.然后出去我不想创建/更新/删除用户.我只想登录用户并让User.Identity.IsAuthenticated = true直到他们退出.因此,任何关于如何回溯到更简单的使用Identity框架的方法的想法都将受到极大的赞赏.我最大的问题是我是否会走正路. 编辑: 解决方法
你在用https吗?
如果不删除此行:CookieSecure = CookieSecureOption.Always 也没有必要IUserLoginStore(仅适用于像facebook这样的外部身份验证) 边注: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET进行请求转发
- asp.net-mvc-4 – .NET MVC4 ActionNameSelectorAttribute
- asp.net-mvc – ASP.NET MVC在新窗口中打开pdf文件
- asp.net – 将persistentCookiesOnPassiveRedirects设置为t
- asp.net-mvc – Ninject.MVC3,Nuget,WebActivator哦我的
- asp.net-mvc-2 – 查找要在ASP.NET MVC项目上使用的授权框架
- asp.net – Windows 7 SP1 IIS错误与“无效的应用程序路径”
- 如何在ASP.NET Core 1.0 RC2中加载程序集
- asp.net – JS,图像和CSS被HTTPModule拦截
- ASP.NET -- WebForm -- HttpRequest类的方法和属性
- 如何在ASP.Net Repeater中创建一个三列表
- asp.net – HTTP错误400 – 与Topshelf的OWIN
- asp.net-mvc – System.Web.Optimization与Micro
- asp.net-mvc – 为什么Nant不与TeamCity合作?
- asp.net-mvc – Cookie中的ASP.NET MVC Store Te
- asp.net-mvc-4 – DataAnnotation Range属性的客
- asp.net – .NET中的堆栈溢出将IIS发送到100%的
- 如何确定asp.net网址是否已被“重写”?
- asp.net – 带有内联样式表和媒体查询的Razor视图
- asp.net-mvc – 使用ToString()和@ Html.Display