Xamarin如何添加SSL用户名认证WCF服务的服务引用?

时间:2013-11-12 17:44:55

标签: c# wcf xamarin.android xamarin service-reference

我正在构建一个需要WCF服务的Xamarin应用程序,该服务将使用SSL(用于开发目的的自签名证书)和用户名身份验证进行保护。

该服务有效,在WinForms项目中创建service reference时,一切正常:

        CategoryServiceClient csc = new CategoryServiceClient();
        csc.ClientCredentials.UserName.UserName = "shaun";
        csc.ClientCredentials.UserName.Password = "TEST";

        System.Net.ServicePointManager.ServerCertificateValidationCallback +=
        (se, cert, chain, sslerror) => { return true; };

        var a = csc.TestService();

在Xamarin中,我只能添加Web Reference而不是Service Reference,因此我需要将代码更改为:

    private void TestConnection() {
        CatServiceRef.CategoryService cs = new CatServiceRef.CategoryService();

        cs.Credentials = new Creds();

        System.Net.ServicePointManager.ServerCertificateValidationCallback +=
        (se, cert, chain, sslerror) => { return true; };

        try {

            string s = cs.TestService();
        }
        catch (Exception ex) {

            throw;
        }
    }

    public class Creds : ICredentials {

        public NetworkCredential GetCredential(Uri uri, string authType) {
            return new NetworkCredential("Shaun", "TEST");
        }
    }

但抛出异常:

    System.Web.Services.Protocols.SoapException: The security context token is expired or is not valid. The message was not processed.

我该如何解决?

WCF服务验证码:

    public override void Validate(string userName, string password) {
        if (null == userName || null == password) {
            throw new ArgumentNullException();
        }

        if (!(userName.ToLower() == "shaun" && password == "TEST")) {
            throw new FaultException("Incorrect Username or Password");
        }
    }

0 个答案:

没有答案