0

我的 Web 服务器上有一个传统的 Xml Web 服务。它有一个服务调用 Main Service。下面给出了类的概述。

[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;
        }
    }
4

1 回答 1

1

在您正在使用的客户端代码中var key=mainService.symmetricKey;

但是 symmetricKey 是私有的byte[],不能在其 contianing 类型之外访问。所以你将不得不使用public property SymmetricKey这样的

var key=mainService.SymmetricKey;

更新 :

我怀疑导致问题的线路一定是这个

        SymmetricKey = rijndaelManaged.Key;

CreateKeys 方法中的 catch 块也通过 throw 冒泡异常;

于 2012-07-12T18:04:45.787 回答