ASP.Net xml Web服务属性返回null值

时间:2012-07-12 18:00:10

标签: c# asp.net web-services cryptography

我的Web服务器上有一个传统的Xml Web服务。它有一个服务调用主服务。类的概述如下。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
    // [System.Web.Script.Services.ScriptService]
public class MainService : System.Web.Services.WebService
{


    private byte[] symmetricKey;
    private byte[] symmetricIv;

    public byte[] SymmetricKey
    {

        get { return symmetricKey; }
        set { symmetricKey = value; }
    }
    public byte[] SymmetricIv
    {
        get { return symmetricIv; }
        set { symmetricIv = value; }
    }

    //Constructor

    public MainService()
    {
        this.GetDatabaseInstance();
    }

//Some Web Methods Heare to create the Symmetric Key
}

在我的客户端,我有一个方法,我有以下措辞

MainService mainService=new MainsService();

//Calling to Some web method to create symmetric keys and assign those properties

//Now trying to access those properties

var key=mainService.SymmetricKey;

当我访问它时,变量“key”获取空值。但我调试了Web服务类。私有变量正在获取值。为什么客户端代码上没有值?可能属性是设置引用而没有设置值?因为我可以通过Web方法返回OUT变量来访问变量值?任何人都可以解释一下答案吗?

以下方法是创建密钥的网络方法

    [WebMethod]
    //create a symmetric key and encript it using clients public key and return values
    public bool CreateKeys(byte[] exponent, byte[] modulus)
    {
        try
        {
            //Create a new instance of the RSACryptoServiceProvider class.
            var rsaCryptoServiceProvider = new RSACryptoServiceProvider();

            //Create a new instance of the RSAParameters structure.
            RSAParameters rsaKeyInfo = new RSAParameters();

            //Set RSAKeyInfo to the public key values. 
            rsaKeyInfo.Modulus = modulus;
            rsaKeyInfo.Exponent = exponent;

            //Import key parameters into RSA.
            rsaCryptoServiceProvider.ImportParameters(rsaKeyInfo);

            //Create a new instance of the RijndaelManaged class.
            var rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.GenerateKey();
            rijndaelManaged.GenerateIV();

            //save  the symmetric key and IV
            SymmetricKey = rijndaelManaged.Key;
            SymmetricIv = rijndaelManaged.IV;

            //Encrypt the symmetric key and IV.
            encryptedSymmetricKey = rsaCryptoServiceProvider.Encrypt(rijndaelManaged.Key, false);
            encryptedSymmetricIv = rsaCryptoServiceProvider.Encrypt(rijndaelManaged.IV, false);

            return true;
        }
        catch (Exception)
        {
            throw;
        }
    }

1 个答案:

答案 0 :(得分:1)

在您使用var key=mainService.symmetricKey;

的客户端代码中

但symmetricKey是私有byte[],无法在其连续类型之外访问。所以你必须像这样使用public property SymmetricKey

var key=mainService.SymmetricKey;

更新:

我怀疑导致这个问题的路线必须是这个

        SymmetricKey = rijndaelManaged.Key;

同样,CreateKeys方法中的catch块通过throw来补充异常​​;