Ideas and thoughts about Microsoft Identity, C# development, cabbages and kings and random flotsam on the incoming tide
Wednesday, February 22, 2012
WIF : Web Platform Installer
Just search on the keyword "identity".
Hey - hey - it's becoming mainstream.
Makes it just that bit easier to download.
The WPI is a really useful tool that collects a pile of Microsoft (and other) downloads in one place. Well worth 5 minutes of your time to go and have a look!
Enjopy!
Monday, February 13, 2012
C# : Some basic validation
Doing some basic validation and I found these to be useful:
Check for mandatory fields:
if (String.IsNullOrEmpty (xxx)) … error
Field must be numeric only:
int number;
bool result = Int32.TryParse (xxx, out number);
if (!result) … error
Valid date format:
DateTime date;
bool result = DateTime.TryParse (xxx, out date);
if (!result) … error
Valid email format:
bool result = Regex.IsMatch(xxx, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)
(([\w-]+\.)+))([a-zA-Z]{2,4}[0-9]{1,3})(\]?)$");
if (!result) … error
Enjoy!
WCF : soapUI error "BadContextToken"
While soapUI is a really useful tool for web service unit testing, it doesn’t work with WCF.
In particular, with the default "wsHttpBinding", you get the message "BadContextToken".
Mr. Google comes back with tons of results about why soapUI doesn’t play nicely with WCF and recommends WCF Storm which looks good but it’s not free.
So a whack of investigation later:
Add the following binding into the <bindings> section of your web.config:
<!-- soapUI -->
<wsHttpBinding>
<binding name="wsHttpBindingNoSecurity">
<security mode="None">
<transport clientCredentialType="None" />
<message establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
Add the following "bindingConfiguration” parameter to your <endpoint> section:
binding="wsHttpBinding" bindingConfiguration="wsHttpBindingNoSecurity"
The WCF service has an endpoint like:
blahblah.svc
For soapUI you need to change this to blahblah.svc?wsdl when you “Add WSDL”.
Once you’ve added the service to soapUI, click the default operation and then click the “WS-A” tab at the bottom.
Click “Enable/disable WS-A addressing”.
Click “Add default wsa:Action”.
Click “Add default wsa:To”.
And (finally) success!
REMEMBER: Every time you change the web.config, you need to right-click the soapUI service and “Update Definition” or F5!
Enjoy!
Wednesday, February 08, 2012
Visual Studio : Showing the active file in the Solution Explorer tree
Something I've always thought would be really useful.
Turns out the way to do this is via:
Tools / Options / Projects and Solutions / General / Click "Track Active Item in Solution Explorer".
WTF isn't this the default?
Enjoy!
Thursday, January 26, 2012
c# : Nullable DateTime
So there I was trying to convert a nullable date time "DateTime?" to DateTime.
Then I got the error:
"Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)"
stackoverflow to the rescue and you need to use the null-coalescing operator.
So the code ended up like:
DateTime x = DateTime y ?? DateTime.Now;Love those "??".
Aside: When I tried to find the article again for this blog, found that searching Google for "??" is an interesting exercise.
Enjoy!
Thursday, December 22, 2011
C# : Counting "rows" in an XML structutre
The structure looks like:
Table
Row
Info1/
Info2/
...
/Row
/Table
I needed to find out how many rows there were.
Mr. Google to the rescue and the solution is:
XmlDocument readTable = new XmlDocument();Refer XPath Examples for the syntax of more kinds of searches you can do.
readTable.LoadXml(stringXml);
int rowCount = readTable.SelectNodes("Table/Row").Count;
Enjoy!
Friday, December 02, 2011
ASP : The Web Form equivalent of MessageBox
When you are writing a Windows application, the ubiquitous MessageBox is extremely useful for popping up a quick debug message but it’s not available for ASP.NET Web Forms.
In such cases, use:
Response.Write("<script language='javascript'>alert('Your message');</script>");
Enjoy!
Wednesday, November 30, 2011
ADFS : Display exceptions
Uncomment this piece:
!-- Display the exception message on the error page. Uncomment this, or add the key below to your
app settings if you want to see the exception message. The exception messages are localized in the
language of the server.--
add key="displayExceptions"
The web.config you need is here:
C:\inetpub\adfs\ls
So I changed the code in FormsSignIn to use an exception called exp instead of ex resulting in the error below.

Enjoy!
Thursday, November 24, 2011
Visual Studio : Web deploy - Files\IIS\Microsoft was unexpected at this time
Then tried to deploy it via:
xxx.deploy.cmd /T
The /T does a whatif - always a good idea to check first!
Then run the following when you are happy:
xxx.deploy.cmd /Y
Got the error - "Files\IIS\Microsoft was unexpected at this time". WTF?
First off when you set the environment variable as per the txt file:
set MSDeployPath="C:\Program Files\IIS\Microsoft Web Deploy V2\"
Do NOT put the quotes in and remove the end \ i.e.
set MSDeployPath=C:\Program Files\IIS\Microsoft Web Deploy V2
What the script does is search for this variable and if it's missing, it tries to find the path in the registry via this code:
if "%MSDeployPath%" == "" (
for /F "usebackq tokens=1,2,*" %%h in (`reg query "HKLM\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" /s ^
findstr -i "InstallPath"`) do (
if /I "%%h" == "InstallPath" (
if /I "%%i" == "REG_SZ" (
if not "%%j" == "" (
if "%%~dpj" == "%%j" (
set MSDeployPath=%%j
))))))
I have Web Deploy V2 deployed and I don't have such a registry entry? It's this piece of script that produces the error. Just comment it out and all will be sweetness and light.
Enjoy!
Tuesday, November 22, 2011
WIF : The FederatedPassiveSignInStatus Control
WTF - it doesn't do anything.
I set the SignOut action to FederatedPassiveSignOut and had an url for the SignOutPageUrl.
Then I noticed that I'd actually added the control to the Site.Master page so that it would appear on every page on the site. Hmm - but that's not actually a page.
So I removed it and added it to my Home page. Works like a dream.
This may help someone else in the same predicament.
Passive Authentication for ASP.NET with WIF has a good description of SingleSignOut.
Makes the point:
"In more complex scenarios, the same clean-up request should be sent to any other STS involved in the federated session. To that end, the STS would have to have prior knowledge of the clean-up URI for each RP and STS. To support single sign-out, your RPs should be able to process these clean-up requests. Both the FAM and the FederatedPassiveSignInStatus control support this. If you’re using the FAM, the clean-up request can be posted to any URI at the RP and the FAM will process the request and clean up any session cookies. If you’re using the FederatedPassiveSignInStatus control, the clean-up request must be posted to a page that contains the control."
Enjoy!