ASMX用户名和密码安全性

时间:2009-04-20 22:18:32

标签: .net web-services login asmx

我有一个基本的ASMX服务,我正在尝试运行(我宁愿使用WCF,但无法让服务器使用它)。它在没有安全设置的情况下运行良好,但是一旦打开安全性,我就会得到:

  

HTTP请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的验证头是'Basic realm =“Secured area”'。

我想要的是一个简约的向用户询问名称和密码类型的解决方案。

使用intellisense围绕代码进行搜索并没有出现任何看起来像我需要的东西。

This看起来可能有用,但似乎是WCF所以谁知道。


我刚刚意识到我可以将其作为现场演示:

这是服务:http://smplsite.com/sandbox3/Service1.asmx

用户名为testapp,密码为testpw。我需要一个命令行应用程序来调用该服务上的函数。

Befor我添加了安全性,在该URL上运行Add Web Service Reference之后,此行在基本VS项目中工作

new ServiceReference1.Service1SoapClient().HelloMom("Bob");

这是我目前的尝试(这不起作用)

class Program
{
    private static bool customValidation(object s, X509Certificate c, X509Chain ch, SslPolicyErrors e)
    { return true }

    static void Main(string[] args)
    {
         // accept anything
        ServicePointManager.ServerCertificateValidationCallback += 
              new RemoteCertificateValidationCallback(customValidation);

        var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        binding.Security.Transport.Realm = "Secured area";

        // the generated Web Service Reference class
        var client = new ServiceReference1.Service1SoapClient(
            binding,
            new EndpointAddress("https://smplsite.com/sandbox3/Service1.asmx")
            );

        client.ClientCredentials.UserName.UserName = "testapp";
        client.ClientCredentials.UserName.Password = "testpw";

        Console.WriteLine(client.HelloMom("Bob"));
    }
}

编辑: BTW这不是网站或在浏览器中运行,访问代码是 C#命令行应用。此外,身份验证由另一个我无法控制的IIS插件完成。

编辑2:要清楚;我正在寻找的解决方案纯属客户端问题。

编辑3:访问控制是通过.haccess类型的系统,我喜欢这样。  我不希望服务代码进行任何身份验证。

3 个答案:

答案 0 :(得分:12)

编辑:
怎么用这个:

MyWebService svc = new MyWebService();            
svc.Credentials = new System.Net.NetworkCredential(UserID, pwd);
bool result = svc.MyWebMethod();    
OP说这不起作用,现在我发现在他的情况下不会这样。

我们这样做:

public class MyWebService : System.Web.Services.WebService
{
    public AuthenticationHeader AuthenticationInformation;

    public class AuthenticationHeader : SoapHeader
    {
        public string UserName;
        public string Password;
    }

    [WebMethod( Description = "Sample WebMethod." )]
    [SoapHeader( "AuthenticationInformation" )]
    public bool MyWebMethod()
    {
        if ( AuthenticationInformation != null )
        {
            if ( IsUserAuthenticated( AuthenticationInformation.UserName,   
                 AuthenticationInformation.Password, ref errorMessage ) )
            {
                 // Authenticated, do something
            }
            else
            {
                 // Failed Authentication, do something
            } 
        }
        else
        {
                 // No Authentication, do something
        }
    }
}

请注意,您提供了IsUserAuthenticated()。

然后客户端将其称为:

 MyWebService svc = new MyWebService();            
 svc.AuthenticationHeaderValue = new MyWebService.AuthenticationHeader();
 svc.AuthenticationHeaderValue.UserName = UserID;
 svc.AuthenticationHeaderValue.Password = Password;

 bool result = svc.MyWebMethod();

答案 1 :(得分:2)

我要添加一个新的答案,因为我认为这可能有所帮助:

http://intellitect.com/calling-web-services-using-basic-authentication/

我不会在这里复制它,因为我没有做任何事情,只是google它。

答案 2 :(得分:1)

配置:

<binding name="MyBinding" closeTimeout="00:00:30"
      openTimeout="00:00:30" receiveTimeout="00:00:30" sendTimeout="00:00:30"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
      textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
      messageEncoding="Text">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="Transport">
        <transport clientCredentialType="Basic" realm=""/>
      </security>
    </binding>
  </basicHttpBinding>
消费时消耗

proxy.ClientCredentials.UserName.UserName = userName;
proxy.ClientCredentials.UserName.Password = password;

这对我来说很好。

相关问题