我有一个基本身份验证(IIS6)背后的网站。

此站点的一部分调用的Web服务也是站点的一部分,因此也支持基本身份验证。

然而,当发生这种情况时,调用代码会收到401 Authentication Error。

我尝试过几件事,一般建议是这样的代码:

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

但是,这似乎无法解决我的问题。

有什么建议吗?

修改

这似乎是一个不常见的问题,但到目前为止,我找不到任何解决方案。 这是关于该主题的一个主题

有帮助吗?

解决方案

解决方案:(我几乎可以肯定这会对某人有帮助)

请参阅此链接,了解VB中此解决方案的来源(谢谢jshardy!),我所做的只是转换为C#。

NB:您必须在IIS上使用ONLY基本身份验证才能实现此功能,但可以对其进行调整。您还需要传递一个Page实例,或者至少传递Request.ServerVariables属性(如果直接从Page code-behind调用,则使用'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);
    }
}

其他提示

The Line:

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

也许你应该尝试:

s.Credentials = HttpContext.Current.User.Identity;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top