Monday, November 25, 2013

Configuration of Class Library

I have developed a ws client in a class library and the code of the ws client is generated by adding a web references.  When I want to move the ws client from uat environment to production environment, the client does not work at all. The problem is raised since I only update the configuration of app.config  inside the class library. As the ws client is a class library, the configuration in app.config will be ignored. Afterwards, I update the  Reference.cs which is generated by vs.

.Net: This application needs to use a cryptographic key

My application is required to integrated with a HTTPS web services. A popup windows is shown when I execute the console application under debug mode. It  requests some permission for key access. After I grant the access, the application works fine.



 Then, I publish the application to IIS and windows security popup is shown when I browse the site. Actually, the site doesn't work. 


The absence of windows security popup gives me some idea on the permission on existing private key. 

There are two ways to solve above issue.
1. Give permission to this folder C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys directly
2. Go to cmd-> type "mmc"-> add snapin certificate->click folder personal->right click->All Tasks->import your private key->right click your private key->All Tasks->Manage Private Key...->grant the permission.






Friday, September 27, 2013

Soap WS-Security Header : One of "SOAP Header" elements required

I run a C# web service client to connect to a Java web service. Then, it throws a exception: [com.ibm.wsspi.wssecurity.SoapSecurityException:WSEC5048EL One of "SOAP Header" elements required]. As no header is defined in the wsdl, I have no idea about the exception and don't know what should be added to the header. Further, the generated web service client does not have a header property. That means, I cannot add the header to the client directly.

After searching, I found out that WS-Security is a standard. 


Then, I try to find out where I can insert the header to the requested XML. Below it's a one of the solutions that I found out on ASP.NET forum.

WSHeader .cs
 public class WSHeader : SoapExtension
    {
        public bool outgoing = true;
        public bool incoming = false;
        private Stream outputStream;
        public Stream oldStream;
        public Stream newStream;

        // The ChainStream, GetInitializers, Initialize, and ProcessMessage are required
        // for a Soap override.  ChainStream and ProcessMessage are what we need.
        /// <summary>
        /// Override.  Save old stream, create new one.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public override Stream ChainStream(Stream stream)
        {
            // save a copy of the stream, create a new one for manipulating.
            this.outputStream = stream;
            oldStream = stream;
            newStream = new MemoryStream();
            return newStream;
        }
        #region "overrides of no interest"
        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override object GetInitializer(Type serviceType)
        {
            return null;
        }
        public override void Initialize(object initializer)
        {
            return;
        }
        #endregion
        /// <summary>
        /// Return a string version of the XML
        /// </summary>
        /// <returns></returns>
        public string getXMLFromCache()
        {
            newStream.Position = 0; // start at the beginning!
            string strSOAPresponse = ExtractFromStream(newStream);
            return strSOAPresponse;
        }
        /// <summary>
        /// Transfer the text from the target stream from the 
        /// current position.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        private String ExtractFromStream(Stream target)
        {
            if (target != null)
                return (new StreamReader(target)).ReadToEnd();
            return "";
        }
        /// <summary>
        /// Override.  Process .AfterSerialize and .BeforeDeserialize
        /// to insert a non-standard SOAP header with username and 
        /// password (currently hard-coded).
        /// </summary>
        /// <param name="message"></param>
        public override void ProcessMessage(SoapMessage message)
        {
            StreamReader readStr;
            StreamWriter writeStr;
            string soapMsg1;
            XmlDocument xDoc = new XmlDocument();
            // a SOAP message has 4 stages.  We're interested in .AfterSerialize
            switch (message.Stage)
            {
                case SoapMessageStage.BeforeSerialize:
                    break;

                case SoapMessageStage.AfterSerialize:
                    {
                        // Get the SOAP body as a string, so we can manipulate...
                        String soapBodyString = getXMLFromCache();

                        // Strip off the old header stuff before the message body
                        // I'm not completely sure, but the soap:encodingStyle might be
                        // unique to the WebSphere environment.  Dunno.
                        String BodString = "<soap:Body>";
                        int pos1 = soapBodyString.IndexOf(BodString) + BodString.Length;
                        int pos2 = soapBodyString.Length - pos1;
                        soapBodyString = soapBodyString.Substring(pos1, pos2);
                        soapBodyString = "<soap:Body>" + soapBodyString;

                        // Create the SOAP Message 
                        // It's comprised of a <soap:Element> that's enclosed in <soap:Body>. 
                        // Pack the XML document inside the <soap:Body> element 
                        //String xmlVersionString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                        String soapEnvelopeBeginString = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://tempuri.org/\" xmlns:types=\"http://tempuri.org/encodedTypes\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
                        String soapEnvHeaderString = "<soap:Header><Security><UsernameToken><Username>";
                        String soapEnvHeaderString2 = "</Username><Password>";
                        String soapEnvHeaderString3 = "</Password></UsernameToken></Security></soap:Header>";

                        Stream appOutputStream = new MemoryStream();
                        StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
                        //soapMessageWriter.Write(xmlVersionString);
                        soapMessageWriter.Write(soapEnvelopeBeginString);
                        //// The heavy-handed part - forcing the right headers AND the uname/pw :)
                        soapMessageWriter.Write(soapEnvHeaderString);
                        soapMessageWriter.Write(ConfigurationManager.AppSettings["WSUserName"]);
                        soapMessageWriter.Write(soapEnvHeaderString2);
                        soapMessageWriter.Write(ConfigurationManager.AppSettings["WSPassword"]);
                        soapMessageWriter.Write(soapEnvHeaderString3);
                        // End clubbing of baby seals
                        // Add the soapBodyString back in - it's got all the closing XML we need.
                        //soapBodyString = dammit;
                        soapMessageWriter.Write(soapBodyString);
                        // write it all out.
                        soapMessageWriter.Flush();
                        appOutputStream.Flush();
                        appOutputStream.Position = 0;
                        StreamReader reader = new StreamReader(appOutputStream);
                        StreamWriter writer = new StreamWriter(this.outputStream);
                        writer.Write(reader.ReadToEnd());
                        writer.Flush();
                        appOutputStream.Close();
                        this.outgoing = false;
                        this.incoming = true;
                        break;
                    }
                case SoapMessageStage.BeforeDeserialize:
                    {
                        //Make the output available for the client to parse...
                        readStr = new StreamReader(oldStream);
                        writeStr = new StreamWriter(newStream);
                        soapMsg1 = readStr.ReadToEnd();
                        xDoc.LoadXml(soapMsg1);
                        soapMsg1 = xDoc.InnerXml;
                        writeStr.Write(soapMsg1);
                        writeStr.Flush();
                        newStream.Position = 0;
                        break;
                    }
                case SoapMessageStage.AfterDeserialize:
                    break;
                default:
                    throw new Exception("invalid stage!");
            }
        }
    }

Add following to web.config 
<system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="TDCWS.WSHeader,TDCWS" priority="1" group="Low"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>


Hope that can help!

References:

Monday, August 12, 2013

TFS: Access to the path is denied

I want to download latest version from TFS 2012 to test the project is uploaded to source control successfully or not. When I run the project, an error is occurred.


After a while, I discover the file property of the project is read-only. Hence, access is denied.

Then, I just wonder why the project become read-only. It is because I haven't check out the project through TFS before the download. So, please remember to check out the project via TFS or just uncheck the read-only properties of the project.




Thursday, June 20, 2013

href vs onclick

I would like to generate a link dynamically. Then, a javascript function is used to redirect to another page. original: It can really redirect the page, but IE doesn't indicate that the page is loading. update: It works fine under IE which can show the page is loading.

Friday, April 26, 2013

RegisterArrayDeclaration

I would like to use ClientScript.RegisterArrayDeclaration to declare an array in client side.So that, it looks like following code.
Client side:
var CheckBoxIDs = new Array(
'ctl00_MainContent_GridViewList_ctl02_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl03_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl04_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl05_CheckBoxItem', 
'ctl00_MainContent_GridViewList_ctl06_CheckBoxItem');

Server side:
ClientScript.RegisterArrayDeclaration("CheckBoxIDs ", checkboxID);

It works fine. However, I need to add elements into the array dynamically. Instead of using ClientScript, ScriptManageer should be used this time.

ScriptManager RegisterArrayDeclaration

Wednesday, April 24, 2013

SerializationException: VewState

In order to save temporary data within a .aspx page, ViewState is my choice.  The data is a List of custom object.
ViewState["TempData"] = new List<ABC>();
Unfortunately, it throws an SerizationException. This error message shows that the class is not serializable. After I add "[Serializable] "  to the class, the problem is solved. But, I don't know why the class needs to be serialized. 

This issue is related to how ViewState packs data into a hidden field. ViewState serialized data into based 64 string and then stored into hidden form field. Later, page can get back state data by deserialized the hidden field.


Understanding ASP.NET View State
[ASP.NET] 利用ViewState保存自訂物件

Wednesday, April 17, 2013

Allow Remote WMI connection in Windows Servers 2008 R2

I wanna a C# program to get disk space of a remote server whose OS is Windows Server 2008 R2. The program and the server are under different domains. I use server's domain account to authenticate the connection. 

Unluckily, the most different part of this task is the configuration of enabling remote WMI connection, not the program to get a disk space. Two error codes:  

=> The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)  
=> Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))  

I have spent much time on the first one and it seems there are some thing wrong with my config. Here are some steps I still remember:

1. Allow WMI to communicate through firewall



2. Enable "remote launch" and "remote activation" in Access Permission of Component Services



3. Enable "remote access" in Access Permission of Component Services



4. Enable "remote enable" in WMI Control Properties



You can simply test WMI connection by using wmic command
wmic /node:localhost /user:administrator /password:1234 bios get serialnumber 

Monday, February 25, 2013

Enable DataSet View in Reporting Services

I would like to enable a view to display DataSet in a report. But, I go to Tab [View] and then search it through [Other Windows]. No relevant window can be found. After googling, the solution is View -> Reporting Data (Ctrl + Alt + D)