How to get an already (basic) authenticated context to call a web service for the same authentication?

I have a site based on basic authentication (IIS6).

Part of this site calls the web service, which is also part of the site and therefore for basic authentication.

However, when this happens, the calling code receives an authentication error 401.

I tried a couple of things, with a general recommendation like this:

Service.ServiceName s = new Service.ServiceName();
s.PreAuthenticate = true;
s.Credentials = System.Net.CredentialCache.DefaultCredentials;
s.Method("Test");

However, this does not seem to be my problem.

Any tips?

Edit

This seems to be not uncommon, but so far I have not found solutions. Here is one thread on the topic.

+3
source share
2 answers

Solution: (I'm pretty sure this will help someone)

VB ( jshardy!), , , #.

NB: IIS, , , . , , , Request.ServerVariables( 'this', ). , , , , .

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);
    }
}
+3

:

s.Credentials = System.Net.CredentialCache.DefaultCredentials();

, :

s.Credentials = HttpContext.Current.User.Identity;
0

Source: https://habr.com/ru/post/1698847/


All Articles