Non funziona perché quando vai a cercare quel file condiviso ti "logghi" come utente locale della macchina (se sei su IIS allora ti logghi sulla macchina in remoto come utente ASPNET o NETWORK SERVICE a seconda del sistema che usi).
Per "bypassare" il problema allora, devi validare il context attivo della richiesta come altro utente. Tempo fa avevo già avuto un problema simile, l'avevo risolto riutilizzando del codice trovato in rete:
codice:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Principal;
using System.Runtime.InteropServices;
public partial class paginaProva : System.Web.UI.Page
{
private enum LogonTypes : uint
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
NetworkCleartext = 8
}
private enum LogonProviders : uint
{
Default = 0
}
protected void Page_Load(object sender, EventArgs e)
{
if ( ImpersonateUserOnContext("", "Administrator", "a") ) {
FileInfo file = new FileInfo(@"\\10.1.8.15\CDG\File1.txt");
Response.Write(file.Exists.ToString());
}
}
public static bool ImpersonateUserOnContext(string domain, string username, string password)
{
bool r = false;
IntPtr token;
bool logged = LogonUser(
username,
domain,
password,
LogonTypes.Interactive,
LogonProviders.Default,
out token);
if (logged)
{
ImpersonateLoggedOnUser(token);
CloseHandle(token);
r = true;
}
return r;
}
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string domain,
string username,
string password,
LogonTypes logonType,
LogonProviders logonProvider,
out IntPtr token);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool ImpersonateLoggedOnUser(IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
}
Prova ad eseguire questo file.. prova con la password giusta e poi con una sbagliata. Vedrai che ti verrà segnalato true/false a seconda dei casi.
Fammi sapere!