简体   繁体   English

即使删除用户,ASP.Net Identity Identity.IsAuthenticated仍然是正确的

[英]ASP.Net Identity Identity.IsAuthenticated remains true, even after deleting user

I have implemented ASP.Net Identity after following the sample code here: https://github.com/rustd/AspnetIdentitySample 我在下面的示例代码后实现了ASP.Net Identity: https//github.com/rustd/AspnetIdentitySample

In my implementation I check if a user is authenticated - this is called from a FilterAttribute on my MVC Controllers; 在我的实现中,我检查用户是否经过身份验证 - 这是从我的MVC控制器上的FilterAttribute调用的; the idea is i want to confirm they are still auth'ed before serving up the page. 这个想法是我想确认他们在提供页面之前仍然是auth'ed。

So in my filter, the following code eventually gets called: 所以在我的过滤器中,最终会调用以下代码:

_authenticationManager.User.Identity.IsAuthenticated;

_authenticationManager is here: _authenticationManager在这里:

private IAuthenticationManager _authenticationManager
{
    get
    {
        return _httpContext.GetOwinContext().Authentication;
    }
}

The _httpContext is passed into the constructor of my identityProvider class. _httpContext被传递到我的identityProvider类的构造函数中。

Now - once I have logged in, _authenticationManager.User.Identity.IsAuthenticated; 现在 - 一旦我登录, _authenticationManager.User.Identity.IsAuthenticated; returns true as expected. 按预期返回true

However, during development, i dumped and re-seeded my database, without adding a user. 但是,在开发期间,我倾倒并重新播种了我的数据库,而没有添加用户。 So effectively, I have deleted the IdentityUser - yet _authenticationManager.User.Identity.IsAuthenticated; 如此有效,我删除了IdentityUser - 但是_authenticationManager.User.Identity.IsAuthenticated; STILL returns true STILL返回true

any idea why this is? 知道为什么会这样吗? I can only assume it's somehow checking a cookie, rather than actually looking at the DB. 我只能假设它以某种方式检查cookie,而不是实际查看数据库。 is this correct? 它是否正确?

Or have i messed up my implementation..... 或者我搞砸了我的实施.....

This does not make IsAuthenticated a security hole. 这不会使IsAuthenticated成为安全漏洞。 Let's look at the actual authentication process. 我们来看看实际的身份验证过程。

  1. You setup some stuff in your web.config around where the login page is, how long the login is good for and whether or not to use sliding expiration (should the time be extended if the user is active on your site) 您在web.config中设置了一些内容,包括登录页面所在的位置,登录有多长时间以及是否使用滑动过期(如果用户在您的站点上处于活动状态,则应该延长时间)

  2. User comes to your site, enters their username and password. 用户来到您的网站,输入他们的用户名和密码。

  3. That information is posted to your server. 该信息将发布到您的服务器。 You take that information, verify that it is correct (authenticate). 您获取该信息,验证它是否正确(验证)。 If it is correct, the server then issues an encrypted cookie known as the FormsAuthenticationTicket Note - this could have a different name in the new Identity stuff, but the same principle. 如果它是正确的,服务器然后发出一个加密的cookie,称为FormsAuthenticationTicket 注意 - 这可能在新的标识内容中有不同的名称,但原理相同。

  4. The cookie's contents includes items such as the user name and expiration date of the login. cookie的内容包括用户名和登录的到期日期等项目。

  5. On each request, the server looks at the cookie collection for the authentication cookie. 在每个请求中,服务器查看cookie集合以获取身份验证cookie。 If found, it decrypts it, reads the values and determines if this is still a valid cookie (expiration time). 如果找到,它会对其进行解密,读取值并确定它是否仍然是有效的cookie(到期时间)。 Once it has the user information from the cookie, the server can use this information to determine if the user is authorized for the resource requested (look up by username). 一旦它具有来自cookie的用户信息,服务器就可以使用该信息来确定用户是否被授权获得所请求的资源(通过用户名查找)。

5a. 5A。 If the cookie is not present, or has expired, then the user is redirected back to the login page. 如果cookie不存在或已过期,则将用户重定向回登录页面。

6.When the user logs out, the cookie is deleted from the cookie collection. 6.当用户注销时,cookie将从cookie集合中删除。 Now, if the user tries to go to a resource that is for authorized users only, then the server ends up at 5a above. 现在,如果用户尝试转到仅供授权用户使用的资源,则服务器将在上面的5a处结束。

So, in your case, you deleted a user manually. 因此,在您的情况下,您手动删除了用户。 This does not change the fact that this user has previously been authenticated with a still valid cookie. 这并不会改变此用户之前使用仍然有效的cookie进行身份验证的事实。 Therefore, IsAuthenticated is returning the expected value. 因此, IsAuthenticated返回预期值。 The user has authenticated before you changed his user status. 在更改用户状态之前,用户已经过身份验证。 IsAuthenticated does not mean, is this user still valid in my database. IsAuthenticated并不意味着,此用户在我的数据库中是否仍然有效。

If you are going to be running a site where you are constantly deleting/deactivating users, then override the OnRequestAuthorization method of the AuthorizeAttribute to look and see if the user is actually still in the database. 如果您要运行一个不断删除/停用用户的站点,则覆盖AuthorizeAttributeOnRequestAuthorization方法以查看该用户是否确实仍在数据库中。 Also, note that if the username is not present (because you deleted it), then any look ups for role / userId will fail. 另请注意,如果用户名不存在(因为您已将其删除),则对role / userId的任何查找都将失败。 You can catch that exception / failure and return the property unauthorized response. 您可以捕获该异常/失败并返回属性未经授权的响应。

暂无
暂无

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

相关问题 ASP.Net Web API中的Identity.IsAuthenticated返回false - Identity.IsAuthenticated return false in an ASP.Net Web API ASP.NET设置用户身份并且IsAuthenticated = true - ASP.NET Set User Identity and IsAuthenticated = true 即使在使用asp.net core 2.2中的PasswordSignInAsync成功登录后,User.Identity.IsAuthenticated也始终返回false - User.Identity.IsAuthenticated always returns false even after successfully logged in using PasswordSignInAsync in asp.net core 2.2 在 asp.net 核心成功登录后,User.Identity.IsAuthenticated 在非身份验证方法中为 false - User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core 用户名+ [空格]导致login.aspx重定向,但Identity.IsAuthenticated为TRUE - Username + [space] causes login.aspx redirect but Identity.IsAuthenticated is TRUE ASP.NET Core Identity 中删除登录用户的策略 - Strategies for deleting a logged-in user in ASP.NET Core Identity 自定义的ASP.Net Identity登录有效,但IsAuthenticated无效 - Customized ASP.Net Identity login works, but IsAuthenticated doesn't asp.net MVC多租户:是否有一种方法可以覆盖/影响User.Identity.IsAuthenticated? - asp.net MVC Multi-tenancy: Is there a way to override/influence User.Identity.IsAuthenticated? 需要帮助使用Asp.Net MVC Razor对User.Identity.IsAuthenticated - Need Help User.Identity.IsAuthenticated using Asp.Net MVC Razor 使用Asp.net Identity删除角色 - Deleting roles with Asp.net Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM