前缀:请不要纠缠在should I do this
,也不要被支持。MicrosoftUnsafe
正是为此目的提供了权限集
使用 PERMISSION_SET = UNSAFE 创建的 CLR 程序集可能能够访问外部系统资源、调用非托管代码并获取系统管理员权限。
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-assembly-transact-sql
现在,进入问题:
因此,传统观点认为,当人们希望使用不安全的代码块时,例如
unsafe {
fixed (char* passwordChars = password) {
var securePassword = new SecureString(passwordChars, password.Length);
securePassword.MakeReadOnly();
return securePassword;
}
}
只需选中Build选项中的复选框并关闭一个小跑:
然而,当一个人在构建一个 SSDTSqlProj
项目时,我完全不明白人们是如何做到的。这是该项目类型的构建 ( SQLCLR Build ) 选项:
那么,除了通常的“危险,危险,罗宾逊”类型的警告之外,这甚至可以用于SqlProj
项目类型吗?是的,我正在构建不安全的代码,是的,我正在签署代码,使用非对称密钥并使用Unsafe
附加权限登录。
抛开这些不谈,你到底是怎么把它传递到编译器命令行的?另外,sqlproj
如果需要修改文件,我也不反对。
想法
If I have to I suppose I might be able create another assembly, outside of the SqlProj
format with this code in and load that to Sql Server as an assembly that I then reference. It just seems odd that the SqlProj
project format specifically prohibits this flag being set.
The UI will not expose the option, but the project file still supports it (along with any other option you can pass to the compiler).
In VS, right-click the project and choose "Unload Project", then right-click again and choose "Edit project". In the main <PropertyGroup>
block, add
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Save, reload the project once more, and it should now build unsafe
blocks without complaint.
请注意,PERMISSION_SET = UNSAFE
这比 C# 所说的“不安全”要广泛得多。即使在经过验证的托管代码中做一些可能会损害引擎的稳定性或吞吐量(如线程)的PERMISSION_SET = SAFE
事情也是被禁止的(在代码访问安全性被放弃并使事情复杂化之前)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句