Come posso ottenere un contesto autenticato (di base) per chiamare un servizio Web dietro la stessa autenticazione?
-
05-07-2019 - |
Domanda
Ho un sito dietro l'autenticazione di base (IIS6).
Parte di questo sito chiama un servizio web che è anche parte del sito e quindi anche dietro l'autenticazione di base.
Tuttavia, quando ciò accade, il codice chiamante riceve un errore di autenticazione 401.
Ho provato un paio di cose, con la raccomandazione generale che è il codice come questo:
Service.ServiceName s = new Service.ServiceName();
s.PreAuthenticate = true;
s.Credentials = System.Net.CredentialCache.DefaultCredentials;
s.Method("Test");
Tuttavia, questo non sembra risolvere il mio problema.
Qualche consiglio?
Modifica
Questo sembra essere un problema non insolito, ma finora non ho trovato soluzioni. Ecco una discussione sull'argomento.
Soluzione
Soluzione: (sono quasi certo che questo aiuterà qualcuno)
Vedi questo link per l'origine di questa soluzione in VB ( grazie jshardy!), tutto quello che ho fatto è stato convertire in C #.
NB: Devi utilizzare SOLO l'autenticazione di base su IIS per farlo funzionare, ma probabilmente può essere adattato. Devi anche passare un'istanza di Page nella, o almeno la proprietà Request.ServerVariables (o usare 'this' se chiamato direttamente da una pagina code-behind). Lo metterei in ordine e probabilmente eliminerei l'uso dei riferimenti, ma questa è una traduzione fedele della soluzione originale e puoi apportare tutte le modifiche necessarie.
public static void ServiceCall(Page p)
{
LocalServices.ServiceName s = new LocalServices.ServiceName();
s.PreAuthenticate = true; /* Not sure if required */
string username = "";
string password = "";
string domain = "";
GetBasicCredentials(p, ref username, ref password, ref domain);
s.Credentials = new NetworkCredential(username, password, domain);
s.ServiceMethod();
}
/* Converted from: http://forums.asp.net/t/1172902.aspx */
private static void GetBasicCredentials(Page p, ref string rstrUser, ref string rstrPassword, ref string rstrDomain)
{
if (p == null)
{
return;
}
rstrUser = "";
rstrPassword = "";
rstrDomain = "";
rstrUser = p.Request.ServerVariables["AUTH_USER"];
rstrPassword = p.Request.ServerVariables["AUTH_PASSWORD"];
SplitDomainUserName(rstrUser, ref rstrDomain, ref rstrUser);
/* MSDN KB article 835388
BUG: The Request.ServerVariables("AUTH_PASSWORD") object does not display certain characters from an ASPX page */
string lstrHeader = p.Request.ServerVariables["HTTP_AUTHORIZATION"];
if (!string.IsNullOrEmpty(lstrHeader) && lstrHeader.StartsWith("Basic"))
{
string lstrTicket = lstrHeader.Substring(6);
lstrTicket = System.Text.Encoding.Default.GetString(Convert.FromBase64String(lstrTicket));
rstrPassword = lstrTicket.Substring((lstrTicket.IndexOf(":") + 1));
}
/* At least on my XP Pro machine AUTH_USER is not set (probably because we're using Forms authentication
But if the password is set (either by AUTH_PASSWORD or HTTP_AUTHORIZATION)
then we can use LOGON_USER*/
if (string.IsNullOrEmpty(rstrUser) && !string.IsNullOrEmpty(rstrPassword))
{
rstrUser = p.Request.ServerVariables["LOGON_USER"];
SplitDomainUserName(rstrUser, ref rstrDomain, ref rstrUser);
}
}
/* Converted from: http://forums.asp.net/t/1172902.aspx */
private static void SplitDomainUserName(string pstrDomainUserName, ref string rstrDomainName, ref string rstrUserName)
{
rstrDomainName = "";
rstrUserName = pstrDomainUserName;
int lnSlashPos = pstrDomainUserName.IndexOf("\\");
if (lnSlashPos > 0)
{
rstrDomainName = pstrDomainUserName.Substring(0, lnSlashPos);
rstrUserName = pstrDomainUserName.Substring(lnSlashPos + 1);
}
}
Altri suggerimenti
The Line:
s.Credentials = System.Net.CredentialCache.DefaultCredentials();
Forse dovresti provare:
s.Credentials = HttpContext.Current.User.Identity;