C# SQL Server 2008文件流模拟访问被拒绝错误
我一直在尝试使用SQLServer2008文件流和模拟技术将文件上载到数据库,以将文件保存在文件系统中,但我不断遇到拒绝访问错误;即使我已经将模拟用户的权限设置为Filestream文件夹(C:\SQLFILESTREAM\Dev_DB)。调试代码时,我发现服务器返回一个unc路径(\server\u Name\MSSQLSERVER\v1\Dev\u LMDB\dbo\FileData\File\u Data\13C39AB1-8B91-4F5A-81A1-940B58504C17),无法通过windows资源管理器访问该路径。 我的web应用程序托管在本地计算机(Windows 7)上。SQL Server位于远程服务器(Windows Server 2008 R2)上。Sql身份验证用于调用存储过程 下面是我用来执行上述操作的代码C# SQL Server 2008文件流模拟访问被拒绝错误,c#,sql-server-2008,filestream,impersonation,C#,Sql Server 2008,Filestream,Impersonation,我一直在尝试使用SQLServer2008文件流和模拟技术将文件上载到数据库,以将文件保存在文件系统中,但我不断遇到拒绝访问错误;即使我已经将模拟用户的权限设置为Filestream文件夹(C:\SQLFILESTREAM\Dev_DB)。调试代码时,我发现服务器返回一个unc路径(\server\u Name\MSSQLSERVER\v1\Dev\u LMDB\dbo\FileData\File\u Data\13C39AB1-8B91-4F5A-81A1-940B58504C17),无法通过
SqlCommand sqlCmd = new SqlCommand("AddFile");
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@File_Name", SqlDbType.VarChar, 512).Value = filename;
sqlCmd.Parameters.Add("@File_Type", SqlDbType.VarChar, 5).Value = Path.GetExtension(filename);
sqlCmd.Parameters.Add("@Username", SqlDbType.VarChar, 20).Value = username;
sqlCmd.Parameters.Add("@Output_File_Path", SqlDbType.VarChar, -1).Direction = ParameterDirection.Output;
DAManager PDAM = new DAManager(DAManager.getConnectionString());
using (SqlConnection connection = (SqlConnection)PDAM.CreateConnection())
{
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
WindowsImpersonationContext wImpersonationCtx;
NetworkSecurity ns = null;
try
{
PDAM.ExecuteNonQuery(sqlCmd, transaction);
string filepath = sqlCmd.Parameters["@Output_File_Path"].Value.ToString();
sqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()");
sqlCmd.CommandType = CommandType.Text;
byte[] Context = (byte[])PDAM.ExecuteScalar(sqlCmd, transaction);
byte[] buffer = new byte[4096];
int bytedRead;
ns = new NetworkSecurity();
wImpersonationCtx = ns.ImpersonateUser(IMP_Domain, IMP_Username, IMP_Password, LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider.LOGON32_PROVIDER_DEFAULT);
SqlFileStream sfs = new SqlFileStream(filepath, Context, System.IO.FileAccess.Write);
while ((bytedRead = inFS.Read(buffer, 0, buffer.Length)) != 0)
{
sfs.Write(buffer, 0, bytedRead);
}
sfs.Close();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
finally
{
sqlCmd.Dispose();
connection.Close();
connection.Dispose();
ns.undoImpersonation();
wImpersonationCtx = null;
ns = null;
}
}
有人能帮我解决这个问题吗。
谢谢尝试使用LOGON32\u登录\u网络,而不是LOGON32\u登录\u交互。很多年前,我在使用API调用UNC模拟时遇到了这个错误,所以它可能仍然有效。在访问Filestream数据时不支持Sql身份验证
我也有同样的问题。解决方案是选中SQLServer配置管理器的“文件流”选项卡上的所有复选框 以下是MSDN上的说明: 注:第7点和第8点:
我们让远程客户端访问文件流,因此我们需要启用所有复选框,最初我们只选中前三个复选框,这导致了错误。Chris,将其更改为LOGON32\u LOGON\u NETWORK对我来说不起作用。我已经添加了异常详细信息和unc路径。感谢您的回复。因此,这一问题的本质是,您的进程正在呈现一个安全令牌,而系统正在将令牌与ACL进行比较,但没有找到它。这里可能有帮助的是在目标服务器上运行process Monitor,并查找拒绝访问条目的详细信息;我怀疑这个过程可能没有我们所期望的身份。可以在此处找到进程监视器:。模拟具有集成安全性的用户解决了此问题。谢谢
Exception:
Type : System.ComponentModel.Win32Exception, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Message : Access is denied Source : System.Data Help link :
NativeErrorCode : 5
ErrorCode : -2147467259
Data : System.Collections.ListDictionaryInternal
TargetSite : Void OpenSqlFileStream(System.String, Byte[], System.IO.FileAccess, System.IO.FileOptions, Int64)
Stack Trace : at System.Data.SqlTypes.SqlFileStream.OpenSqlFileStream(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize)
at System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize)
at System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access)