Category Archives: Microsoft dotNet

Hidden Lead Bytes in Thumbprint while copying from Windows

Microsoft digs lots of pits, and I keep jumping among them.

Recently, I wrote a program using HttpClient in dotNet to post some data to server through HTTP Post. The server is set with client certificate required.

My designing is simple:

  1. Open X509Store to query the certificate by using thumbprint.
  2. Attach the certificate found in step 1 into WebRequestHandler.
  3. Pass the handler created in step 2 to the instance of HttpClient and send the request.

Required by step 1, I need to type the thumbprint of the certificate into code. The steps I did:

  1. Open certificate from Windows. It’s shown up like this:
  2. Copy the text of Thumbprint and paste it into a notepad.
  3. Replace all spaces to nothing.
  4. Copy the new text into code.

When I run the code, it ended in a strange way. No certificate is found by using the thumbprint I provided. I dig a long time before I found this pit prepared by Microsoft:
The text, copied from the window above (in step 1), contains hidden lead bytes “0x200E”. These bytes won’t display in code view of Visual Studio, nor in Notepad.

After these removed, problem is gone as well.

Web Deploy for Visual Studio

While I’m trying to deploy my site from Visual Studio to a Windows Server 2012 R2 instance, I have to dig a lot for troubleshooting about Web Deploy related problems.


Here are some not listed in Microsoft documents:

  • Visual Studio need both Web Deploy (Web Deployment Agent Service, MsDepSvc) and Web Management Service (WMSVC).
  • You cannot install Web Management Service separately.
  • The easy way to install the both is choose “Web Deploy 3.6 for Hosting Servers” Web Platform Installer.
  • If you selected “Web Deploy 3.6” in another release, without “for Hosting Servers”, Web Management Service will not be installed.
  • If you leave Web Deploy installed without Web Management Service, you could add it by install “Web Deploy 3.6 for Hosting Servers” but IT WILL NEVER WORK! You have to UNINSTALL existed Web Deploy first and then reinstall the “Web Deploy 3.6 for Hosting Servers”.

Div dynamic loading is not supported in Windows IoT (yet?)

Hi. I built an side powered by SignalR recently. The function is easy that dynamically load from a URL provided from SignalR into a div from browser side. It works like a charm in IE, Edge and Chrome. The JavaScript written in the web page is simple:

  • SignalR 2.2.0 and jQuery 2.2.3 are selected;
  • Connect SignalR at beginning;
  • When SignalR disconnected, reconnect;
  • When SignalR event raised, call one method to pull the data from URL specified and set as content of a div; and,
  • Call the method mentioned above at beginning to load the initialization data.

Then, I need to do the same thing in one Raspberry Pi 3. I choose Windows IoT with UWP to build the basis. UWP app is quite easy, only one WebView control is embedded with the starting URL set to the page of that website. According to only Insider Preview version can support Raspberry Pi 3, build 14295 is selected, which is the latest one can be found from official site.

After this app deployed and started in Raspberry Pi 3, it acts quite different. The initialization data is shown without any problem. But no more SignalR event is proceeded. By debugging on the server side, SignalR OnConnected, OnDisconnected and OnReconnected are called repeatedly and frequently.

Due to no more information can be grabbed from app running in Raspberry Pi 3, I have no clue at first. The good thing is I made a similar solution before. The only difference between these 2 apps, is the previous one use iframe instead of div. Despite of no reason observed, after I change the div into iframe and make the data surrounded by html and body tags which filling div previously , the problem vanishes away.

UWP Developing: Don’t forget the firewall on IoT devices

After I deploy my UDP based application to Raspberry Pi 2 running Windows IoT 10.0.10586, this application receives nothing, just like running in a normal Windows computer.

I guess that maybe something related to firewall, but:
1 There is no article related to target that. As the time I’m writing this post, googling “Windows IoT firewall” returns nothing related.
2 There is no page for firewall configuration in Windows IoT utilities site.

Fortunately, a test proved that there is a built in Windows IoT.
By using PowerShell connection, I send the netsh command to create an exception of firewall. It accepted and the my application started to receive package after then.

Related command for reference:
netsh advfirewall firewall add rule name=”A name for this exception” dir=in action=allow protocol=UDP localport=12345

Hope this helps.

UWP Developing: Local network protocal stack issue

I’m working on developing a Universal Windows App (Windows 10 IoT) recently. My app includes a UDP server for receiving packages.

The issue I found is:
When the UWP running on local computer, the datagram sent by local console application cannot be received.

Normally, while developing a socket based network application, another application for making and sending test packages should be prepared. The problem is, for testing UWP application, either the tester should be prepared as UWP application, or this tester can only be used on another computer laying on the same network.

TFS 2015 Upgrading

There are 2 things you should have in mind about TFS 2015 Upgrading.

1 The database upgrading process will cost much more time than upgrading among Update package of TFS 2013.

2 The dedicated SharePoint integration package is removed and M$ did a shit test again. If you, like me, installed TFS on a server other than SharePoint cluster, now you have to install the whole TFS on all SharePoint servers. After the installation, the upgrading wizard will be popped up but it will failed due to no database can be found. You have to choose to install SharePoint integration role yourself. And after that, you will know that TFS SharePoint integration package 2013 will NOT and NOT ABLE to be removed, lol, just another piece of crap.

A way to run nearly all functions of dotNet from SQL Server

Last night, I got a case to write a function in SQL Server 2005 to support the user account and password check against Active Directory. The user requirement is quite clear:

  • Create a scalar-valued function named LDAPUserCheck;
  • Parameter @username nvarchar(MAX) for user name to check;
  • Parameter @password nvarchar(MAX) for password matching the username specified;
  • Return bit 1 if succeeded, or 0 for all other reasons.

After a digging, I found that LDAP password authentication is not supported directly by SQL Server. But SQLCLR is a way to build the native dotnet program into SQL Server. In a new created SQLCLR project in VS 2005, I realized it’s unable to add the reference System.DirectoryServices.AccountManagement, which is required by running the code for Active Directory authentication. But a Web Service is a choice.

My steps to achieve that:

1 Create and deploy a Web Service for the authentication check.

1.1 Create a Web Service project.

1.2 Add System.DirectoryServices.AccountManagement as a reference.

1.3 Add a setting DomainName as string for storing the domain name.

1.4 Add a service like this:

    public class LDAPAuthentication : System.Web.Services.WebService
        static string domainName = Settings.Default.DomainName;

        public bool Check(string userName, string password)
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
                // validate the credentials
                bool isValid = pc.ValidateCredentials(userName, password);
                return isValid;

1.5 Deploy this service and use a application pool running by a domain user. This user should be added to IIS_WPG group in Windows Server 2003.

2 Create a SQLCLR project to call the Web Service.

2.1 Create a SQLCLR project in Visual Studio 2005.

2.2 Add a Web Service reference. It’s named as LDAP in my project.

2.3 Add a User Defined Functions.

    public static SqlBoolean LDAPUserCheck(
        SqlString username, SqlString password)
        using (LDAPAuthentication.LDAP.LDAPAuthentication service = new LDAPAuthentication.LDAP.LDAPAuthentication())
            if (service.Check(username.ToString(), password.ToString()))
                return SqlBoolean.True;
                return SqlBoolean.False;

2.4 Set Permission Level to External in Database page of project setting.

2.5 Build this project to get the dll files. In my case, these files are named LDAPAuthentication.dll and LDAPAuthentication.XmlSerializers.dll.

3 Deploy this project into SQL Server 2005.

3.1 Enable the CLR support in SQL Server 2005 by running this code:

sp_configure 'show advanced options', 1;

sp_configure 'clr enabled', 1;

sp_configure 'show advanced options', 0;

3.2 Create a database for storing this function. Or, you can use any existed database as well. In my case, I created a database “tester”.

3.3 Set trustworthy on this database by running:

ALTER DATABASE [tester] SET trustworthy ON

3.4 Copy the 2 dll files created in step 2 to this server. In my case, these are stored in C:\SQLCLR folder.

3.5 Create assemblies in SQL Server by running:

create assembly [LDAPAuthentication] from 'C:\SQLCLR\LDAPAuthentication.dll' with permission_set = external_access
create assembly [LDAPAuthentication.XmlSerializers] from 'C:\SQLCLR\LDAPAuthentication.XmlSerializers.dll' with permission_set = external_access

3.6 Create function to run the method we’ve created in VS 2005:

CREATE FUNCTION [dbo].[LDAPUserCheck](@username [nvarchar](4000), @password [nvarchar](4000))
EXTERNAL NAME [LDAPAuthentication].[UserDefinedFunctions].[LDAPUserCheck]

Now everything is done. You can call this function like all others created by SQL. Run this for test.

select dbo.LDAPUserCheck('myusername','mypassword')