ASMX用户名和密码安全性

BCS*_*BCS 7 .net web-services login asmx

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

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

我想要的是一个简约的用户要求用户输入名称和密码类型的解决方案.

使用intellisense在代码周围徘徊并没有提出任何看起来像我需要的东西.

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


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

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

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

因为我添加了安全性,这条线在运行Add Web Service Reference该URL 后在基本的VS项目中工作

new ServiceReference1.Service1SoapClient().HelloMom("Bob");
Run Code Online (Sandbox Code Playgroud)

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

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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

编辑2:要清楚; 我正在寻找的解决方案是一个纯粹的客户端问题.

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

Moo*_*ose 12

编辑:
如何使用这个:

MyWebService svc = new MyWebService();            
svc.Credentials = new System.Net.NetworkCredential(UserID, pwd);
bool result = svc.MyWebMethod();    
Run Code Online (Sandbox Code Playgroud)

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您提供IsUserAuthenticated().

然后客户端像这样调用它:

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

 bool result = svc.MyWebMethod();
Run Code Online (Sandbox Code Playgroud)