1

I want to know whether filestream in C# uses SQL Server authentication.

Is it possible to connect to the database using filestream in windows authentication by using userid and password?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1400915
  • 1,933
  • 6
  • 29
  • 55

2 Answers2

1

Yes it is mandatory to use windows authentication filestream. Best thing to deal with it is impersonating it.

Declare the following methods inside the class

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwlogonType, int dwlogonProvider, out SafeTokenHandle phtoken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

Then in the method:

public void method()
{

   WindowsImpersonationContext context = null;
   SafeTokenHandle safeTokenHandle;

    bool returnValue = LogonUser(connString[0], connString[1], connString[2], Convert.ToInt32(connString[3]), Convert.ToInt32(connString[4]), out safeTokenHandle);

    WindowsIdentity windowsIdentity = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());

    context = windowsIdentity.Impersonate();

    //Make some operations

    context.undo();

}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
user1907849
  • 960
  • 4
  • 19
  • 42
0

I want to know whether filestream in C# uses SQL Server authentication.

Depend what you think FileStream is. FileStream access via SQL uses whateve rthe SQL connectionuses. FileStream via windows - OBVIOUSLY - has to use windows authentication.

Is it possible to connect to the database using filestream in windows authentication by using userid and password?

Is it possible to connect to a windows share using username and pasword? YES. As long as thos ae are username and password ofa windows user.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • when accessing a SQL-Server`s FILESTREAM your sql-auth is only used to check our table and field permission. the definitv file access is done by the sql server service with the permissions it is running e.g. local service. this means that only this user/service account needs NTFS permissions on the filestream-container – marc.d Jul 24 '12 at 10:07
  • @marc.d half-knowledge. it is possible to expose a FileStream as network share, you know. And then to get the path to a speicific file via SQL, so you can access it directly. This is great, for example for a document repository (Word so to say can read the file without knowing anything aobut SQL), as well as many other scenarios, and in MANY cases I an see that the predominant access form - which you seem to be totally unaware of. http://www.mssqltips.com/sqlservertip/1838/different-ways-to-enable-filestream-feature-of-sql-server-2008/, http://msdn.microsoft.com/de-de/library/bb895239.aspx – TomTom Jul 24 '12 at 10:43
  • Is it possible to use SQL Server authentication? – user1400915 Jul 25 '12 at 08:24
  • As it says in one of the links I provided in tmy last comment - NO. Which is OBVIOUS - it is a WINDOWS file share, windows does not care about sql server authentication. So if you want to do that you HAVE to use windows authentication at least for those users in sql server. – TomTom Jul 25 '12 at 08:26
  • Is there any document from microsoft site to tell that windows authentication is mandatory for accessing filestream feature? – user1400915 Aug 31 '12 at 17:30