简体   繁体   English

ASP.Net Identity失去了模仿

[英]ASP.Net Identity losing Impersonation

I'm having a lot of problems that whenever I call an ASP.Net Identity Async method I get access denied exceptions from SQL server. 我遇到很多问题,每当我调用ASP.Net Identity Async方法时,我都会从SQL服务器获得访问被拒绝的异常。

The following returns a user: 以下内容返回用户:

var user = (from u in ctx.Users
            where u.Id == 1
            select u).FirstOrDefault();

Whereas

var user = await userManager.FindByIdAsync(1);

Triggers an exception 触发异常

System.Data.SqlClient.SqlException: Login failed for user 'DOMAIN\MACHINE$'.

What seems to be happening is we have the following line in our web.config configuration/system.web section 似乎正在发生的事情是我们在web.config配置/ system.web部分中有以下行

<identity impersonate="true" userName="DOMAIN\Domain User" password="xxx" />

This impersonated user has permission to access the SQL Server database but the application pool user does not. 此模拟用户有权访问SQL Server数据库,但应用程序池用户没有。 It seems that whenever I call an async method in ASP.Net Identity, it falls back to the app pool user and loses the impersonation. 似乎每当我在ASP.Net Identity中调用异步方法时,它都会回退到app pool用户并失去模拟。

I did find a similar sounding question with an explanation here https://stackoverflow.com/a/30876110/1093406 我确实在这里找到了一个类似的声音问题和解释https://stackoverflow.com/a/30876110/1093406

Could this also be causing my problem? 这也可能导致我的问题吗?

Is there any way around this apart from changing the app pool user to one that has database access? 除了将应用程序池用户更改为具有数据库访问权限的用户之外,还有什么方法吗?

Is using web.config to set an impersonation user an old way of doing things and now bad practice? 是否正在使用web.config将模拟用户设置为旧的处理方式,现在是不好的做法?

Edit: After further investigation I've found these articles http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx http://blog.codeishard.net/2012/09/17/await-async-mvc-and-impersonation/ 编辑:经过进一步调查后,我发现这些文章http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx http://blog.codeishard.net/2012/09/17/await-async-mvc-and-模拟/

Looks as though using the impersonation is a bad idea unless someone can tell me otherwise. 看起来好像使用模仿是个坏主意,除非有人能告诉我。

Impersonation is no longer supported in Integrated Pipeline modes. 集成管道模式不再支持模拟。 Especially when using async methods. 特别是在使用异步方法时。

The reason is that Impersonation happens on the thread, and when you call an async function, it may actually execute or return on a different thread. 原因是模拟发生在线程上,当你调用异步函数时,它实际上可能在不同的线程上执行或返回。

You should use a WindowsImpersonationContext to wrap your database calls. 您应该使用WindowsImpersonationContext来包装数据库调用。

https://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext(v=vs.110).aspx

using System.Security.Principal;
...

//Get the identity of the current user
IIdentity contextId = HttpContext.Current.User.Identity;
WindowsIdentity userId = (WindowsIdentity)contextId;

//Temporarily impersonate
using (WindowsImpersonationContext imp = userId.Impersonate())
{
    //Perform tasks using the caller's security context
    DoSecuritySensitiveTasks();
}

Make sure that you do this in a using block, since if an uncaught exception occurs in your code, you would end up not restoring the original context, and creating a security issue. 确保在using块中执行此操作,因为如果代码中发生未捕获的异常,您最终将无法恢复原始上下文,并且会产生安全问题。

My solution in the end was to set the user on the App Pool rather than using Impersonation. 我的解决方案最终是将用户设置在App Pool而不是使用Impersonation。 This seems to have exactly the same effect as setting impersonation in the Web.config with equivalent security, just without the problems when Async is being used. 这似乎与在具有相同安全性的Web.config中设置模拟具有完全相同的效果,只是在使用Async时没有问题。

As @Erik said in his answer, it seems that impersonation is no longer supported and according to http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx it was never encouraged in the first place. 正如@Erik在他的回答中所说的那样,似乎不再支持模仿,并且根据http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx,它从未被鼓励过。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM