简体   繁体   English

如何在表单身份验证中将User.Identity.IsAuthenticated的值设置为false

[英]How to set value to User.Identity.IsAuthenticated as false in Forms Authentication

I have a situation where i want to input the details for anonymous Users also. 我有一种情况,我想输入匿名用户的详细信息。 So i have created a user id for them in Profile. 所以我在Profile中为他们创建了一个用户ID。 When i am setting the user details 当我设置用户详细信息时

FormsAuthentication.SetAuthCookie(username, true);

I want the details relating to the user with User.Identity.Name with the username, but the User.Identity.IsAuthenticated to false. 我希望User.Identity.Name与用户有关的详细信息与用户名,但User.Identity.IsAuthenticated为false。 Right now i am getting it as true. 现在我认为它是真的。 I want to set this to true only after a particular login. 我想在特定登录后才将其设置为true。

The FormsIdentity class is hardwired to return true, always. FormsIdentity类是硬连线的,总是返回true。 So this cannot be changed. 所以这不能改变。 Even a GenericIdentity returns true whenever the name is not empty. 只要名称不为空,即使GenericIdentity也会返回true。

A workaround would be to create your own IIdentity implementation, and replace the forms identity in the Application_PostAuthenticateRequest event in Global.asax. 解决方法是创建自己的IIdentity实现,并替换Global.asax中Application_PostAuthenticateRequest事件中的表单标识。 It's a proven and safe solution. 这是一个经过验证的安全解决方案。

Also please consider whether you really want to do this. 另外请考虑您是否真的想要这样做。 It is good practice that unauthenticated users are anonymous, they have no name, and it is very elaborate why the built-in identity classes work the way they do. 优秀的做法是,未经身份验证的用户是匿名用户,他们没有名字,并且内置身份类的工作方式非常复杂。

Unfortunately you cannot modify the User.Identity.IsAuthenticated to false. 遗憾的是,您无法将User.Identity.IsAuthenticated修改为false。 You can create your own cookie or you can assign a string variable for this user to session as a Dictionary. 您可以创建自己的cookie,也可以将此用户的字符串变量作为Dictionary分配给会话。 By this way you can modify the value and manually destroy that value for specific scenarios. 通过这种方式,您可以修改该值并手动销毁特定方案的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OWIN 身份验证成功,但 User.Identity.IsAuthenticated 为 false - OWIN authentication succeeds but User.Identity.IsAuthenticated is false User.Identity.IsAuthenticated始终为false - User.Identity.IsAuthenticated is always false 为什么User.Identity.IsAuthenticated false? - Why User.Identity.IsAuthenticated false? User.Identity.IsAuthenticated始终返回false - User.Identity.IsAuthenticated always returns false User.Identity.IsAuthenticated 是假的,即使我设置了 cookie - User.Identity.IsAuthenticated is false, even when I have a cookie set 登录成功后 User.Identity.IsAuthenticated 为 false - User.Identity.IsAuthenticated is false after successful login SignInManager.PasswordSignInAsync() 成功,但 User.Identity.IsAuthenticated 为 false - SignInManager.PasswordSignInAsync() succeeds, but User.Identity.IsAuthenticated is false User.Identity.IsAuthenticated 在使用 ITFoxtec 的 SAML 2.0 中始终为假 - User.Identity.IsAuthenticated Always false in SAML 2.0 using ITFoxtec PasswordSignInAsync() 成功后 User.Identity.IsAuthenticated 为 false - User.Identity.IsAuthenticated is false after PasswordSignInAsync() succeeded 为什么我得到User.Identity.IsAuthenticated false - Why do I get User.Identity.IsAuthenticated false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM