Ideas and thoughts about Microsoft Identity, C# development, cabbages and kings and random flotsam on the incoming tide
Friday, August 24, 2018
C# : Invalid URI
"Invalid URI : The hostname could not be parsed".
I get this using the URIBuilder class.
Since it was complaining about the hostname I checked the DNS, I checked that I could ping it, I tried the IP address etc. etc.
Eventually worked out that it was because the password contained special characters.
You are apparently supposed to URL encode them.
Just changed the password to use letters and numbers and all was well.
Somewhat misleading error message :-)
Enjoy!
Tuesday, July 03, 2018
C# : The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly
To check all was well, I set VS to break-point on all CLR exceptions and suddenly it came up with the above error.
WTF?
Lots of discussions with Mr. Google and the usual ton of garbage but then I found this.
Run "cmd" as admin.
cd C:\Windows\Inf\.NET Data Provider for SqlServer
That did the trick.
Friday, July 28, 2017
ASP.NET : Misused header name. Make sure request headers are used
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
This is when I use HttpClient with .NET 4.5 and try something like:
client.DefaultRequestHeaders.Add("Contact-Type", ...);
And so began a long and painful journey to figure out to to fix this because the external web API wouldn't work without "Content-Type" as a header.
There is so much garbage out there :-(
After some research, Content-Type is part of Content - the name pretty much implies that - so use HttpContent.
using (var client = new HttpClient())
{
// Adding contentType to client as header gives "Misused header name. Make sure request headers are used
with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent
// objects."
client.DefaultRequestHeaders.Add("Authorization", "Bearer abc...123");
HttpResponseMessage response;
// Construct an HttpContent from the data
HttpContent hc = new StringContent(data);
hc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = client.PostAsync(baseAddress, hc).Result;
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsStringAsync().Result;
}
Enjoy!
Thursday, April 06, 2017
C# : Sending email
So I came up with this.
I used Windows Forms and a very simple button.
This uses the System.Net.Mail.MailMessage class.
Enjoy!
Tuesday, September 13, 2016
AD : Attempt to compromise security (HRESULT: 0x800704F1)
System.DirectoryServices.AccountManagement.PrincipalOperationException: The system detected a possible attempt to compromise security. Please ensure that you can contact the server that authenticated you. (Exception from HRESULT: 0x800704F1) ---> System.Runtime.InteropServices.COMException: The system detected a possible attempt to compromise security. Please ensure that you can contact the server that authenticated you. (Exception from HRESULT: 0x800704F1)
This is described here:
MS16-101: Description of the security update for Windows authentication methods
Essentially, the updates disable the ability of the Negotiate process to fall back to NTLM when Kerberos authentication fails for password change operation.
I found these useful:
Enabling Secure LDAP on Windows Server 2008/2012 Domain Controllers
Enabling Secure LDAP on Windows Server 2008/2012 Domain Controllers: Configuration
There is a good write-up here:
Solution for Problem in Change Password after Windows Security Update
TL;DR
- Enable LDAPS
- Set "Trusted Hosts"
- Revert the update
And so began a journey into the depths of the Internet - or more specifically the huge amount of garbage, rubbish and misinformation out there :-(
One of the problems was the connection string.
I naively assumed that I simply needed to change:
LDAP://fqdn to
LDAPS://fqdn
But from what I can see, there is no actual protocol called LDAPS? It's official name is "Secure LDAP" and the connection string you need is:
LDAP://fqdn:636
The "correct" way to enable it is to install an Enterprise Root CA on a Domain Controller.
That wasn't an option (and a massive overkill) so I started wondering about installing a self-signed certificate on the DC.
Disclaimer: Using self-signed certificates or installing certificates as follows may not be the best solution security wise. Use this at your own risk!
The certificate that you need has to follow certain rules e.g.
- It has to be for "Server Authentication" (OID: 1.3.6.1.5.5.7.3.1)
- The Subject name must match the Fully Qualified Domain Name (FQDN) of the host machine, such as Subject:CN=server.domain.com
- (This is also true for the first name in the Subject Alternative Name (SAN))
- The host machine account needs to have access to the private key
Once you have the .pfx file, and using "mmc" with:
Service Account / Local Computer
Select "Active Directory Domain Services".
and import.
You need this certificate in "NTDS\Personal" and "NTDS\Trusted Root Certification Authorities".
To check this, you need to run "ldp" from the command line.
This should result in:
"Host supports SSL, SSL cipher strength = 256 bits"
If you need to run "ldp" from a non-DC machine, you need Remote Server Administration Tools (RSAT) for Windows Client and Windows Server.
Once "ldp" runs OK, you now have secure LDAP.
Now for the C# .NET side:
Replace the ldapConnection below:
DirectoryEntry ldapConnection = null;with:
try
{
ldapConnection = new DirectoryEntry(LDAP fqdn, user, password);
}
ldapConnection = new DirectoryEntry(LDAP fqdn, user, password,Coming at this from another angle, this code also worked:
Password,AuthenticationTypes.SecureSocketsLayer);
Authenticating a user over LDAP in .Net: LdapConnection vs. PrincipalContext
Footnote: I also found that I could leave out the extra parameter:
"Password,AuthenticationTypes.SecureSocketsLayer"
if the user was "domain\user" rather than "user". Go figure!
Enjoy!
Thursday, November 26, 2015
C# : GetManifestResourceStream returns null on pfx file
The certificate was OK - I could import it, inspect it etc.
WTF? - chat with Mr. Google and turns out you need to right-click on the pfx file in the project / Properties and set "Build Action" to "Embedded Resource".
Enjoy!
Friday, July 04, 2014
Azure : Help me pick an authentication scenario / library
Starting with Azure Active Directory:
Windows Azure Active Directory Authentication Libraries
which covers:
- Windows Azure AD Authentication Library for .NET
- Windows Azure Authentication Library for Windows Store
- Windows Azure AD Node.js Module
Authentication Scenarios for Azure AD
Azure Active Directory Code Samples
Vittorio Bertocci has a few blogs around Azure Directory Authentication Library (ADAL):
Search results for "adal"
Also:
Which Azure authentication library should I choose?
And protocol overview:
Azure Active Directory Authentication Protocols
Enjoy!
Wednesday, May 28, 2014
Visual Studio : Extending the user profile for organisational accounts in Azure
You can see this if you click on the name of the logged-in user once the application is running and you have authenticated.
What if you want to extend this?
The first step is to find out what the attribute is called?
The easiest way to do this is to use the Graph Explorer.
Click "Use Demo Company" then "Get" then click on:
"https://graph.windows.net/GraphDir1.OnMicrosoft.com/users".
You'll get a list of the AAD schema attributes e.g if you want the user department, you'll see the name is "department".
In the VS project under "Models / HomeViewModels.cs" add another line e.g.
public string Department { get; set; }
Under "Views / Home / UserProfile.cshtml" add another line e.g.
This uses Razor syntax - you may have something different but you get the general idea.<td>Department</td> <td>@Model.department</td>
The key to this is under "Controllers / HomeController.cs" where:
UserProfile profile = JsonConvert.DeserializeObjectleverages the power of the JSON library to serialize the attributes you have defined.(responseString);
Job done.
Enjoy!
Monday, March 03, 2014
OWIN : WS Federation support
Using Claims in your Web App is Easier with the new OWIN Security Components
There's a good reference to OWIN in the above.
WS-Federation in Microsoft OWIN Components–a quick start
But what the article doesn't show is how to display the claims.
Dominick blogged on this:
Test driving the WS-Federation Authentication Middleware for Katana
Horrible hack below - yeah I know! - I put it in the About page in the HomeController ...
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
var ctx = Request.GetOwinContext();
var user = ctx.Authentication.User;
var response = ctx.Response;
response.ContentType = "text/html";
if (user.Identity.IsAuthenticated)
{
... code as above
}
Enjoy!
Friday, January 31, 2014
LINQPad : debugging with Visual Studio
Note: If you pay for it, you get Intellisense as well.
Came across a situation where I had a bug and couldn't put my finger on it. Would be ideal to debug / step-through it but you can't ...
Well, actually you can - learn something new every day.
Add these lines to your snippet:
Debugger.Launch();
Debugger.Break();
So obviously LINQPad is running
Start up Visual Studio - the normal Start screen is fine
Debug - Attach to Process
Select LINQPad.exe from the list and Attach
Run your snippet
BINGO - jumps into VS - debug away.
How have I not known about this?
Enjoy!
Wednesday, January 29, 2014
AD : PrincipalOperationException A device attached to the system is not functioning
Got this on a web site - WTF - what device?
Card reader, CD drive ... not on a web site, mate!
Turns out this is during a create on a user or a group in AD where the name is greater than the allowed field length.
So for Create User - the SAMAccountName can't be greater than 20 characters,
There's probably a similar restriction for group since I got the same error.
Good interview question :-)
Enjoy!
Wednesday, January 08, 2014
C# : Cross site scripting and quotes
The site has some forms where you enter user details into text boxes. This is stored in a DB. You can search for users and the results are displayed in a grid e.g. first name, last name, email, roles etc.
So if you entered some Javascript into the last name text box e.g.
script Alert ('XSS error'); /script
(insert your own angle brackets!)
this would be written to the DB and when the grid displayed the next tine, you would see the Alert - the dreaded XSS syndrome.
Microsoft has a library to handle this - the somewhat maligned AntiXSS.
This has methods for HTML encoding, CSS encoding, URL encoding, injection etc.
So e.g. you can use:
AntiXssEncoder.HtmlEncode(lastname, true);
If someone types:
script Alert ('XSS error'); /script
as a last name, this is encoded as:
<script>Alert (‘XSS Error’);</script>
which is harmless.
Now that's all well and good but you will note from the above that the single quote "'" is also encoded.
So what happens when you have someone with a name of O'Reilly?
Bad stuff happens - that's what!
What is saved is:
O'Reilly
There are many Google hits around the fact that the library is actually too strict etc. etc.
What I do is after the encoding, is:
lastname.Replace("& # 3 9 ;''", "'"); // Remove the spaces
So it's back to the single quote.
You could also use something like:
Sanitizer.GetSafeHtmlFragment (lastname);
which removes a few HTML elements like script - so in this case there's just an empty string.
And for injection e.g. LDAP, you can use:
Microsoft.Security.Application.Encoder..LdapFilterEncode("xyz");
Enjoy!
Wednesday, September 04, 2013
C# : Always learn something new
What does ?? mean in C# ?
You can read the full article but the essence is:
"?? provides a convenient way that I can map the null value of a nullable variable onto a specific value
int actualAge = customerAge ?? -1;
The above statement sets the value of actualAge to the value in customerAge unless the value in customerAge is null, in which case it sets it to –1.
if (customerAge == null)
actualAge = -1;
else actualAge = customerAge;
Enjoy!
Wednesday, August 28, 2013
C# : String with spaces is not null or empty
I was checking a text field with
String.IsNullOrEmpty
and I noticed that if you enter spaces, it is neither null or empty!
Mr. Google out of bed bright and early and:
String.IsNullOrEmpty() Check for Space
The solution is the "IsNullOrWhiteSpace(string value)" one.
Enjoy!
Wednesday, July 03, 2013
AD : Locked accounts in Active Directory (AD)
Once upon a time, there was an attribute in AD called:
userAccountControl - ADS_UF_LOCKOUT = 16 (d) 10 (h)
However, in later versions of Windows Server (e.g. 2008), this was moved to:
msDS-User-Account-Control-Computed - UF_LOCKOUT = 16 (d) 10 (h).
BUT there was a slight problem. As the name implies this is a computed attribute i.e. it doesn't actually exist. Rather it is computed on the fly. The implication is that it can't be used in a standard search query.
Hence the curved ball.
Some blogs suggest you can use:
filter = "(&(objectClass=user)(lockoutTime>=0))"
This works in the sense that it refines a potentially huge list to a much smaller one.
However, it is not sufficient. When a user is locked out and then unlocked, this attribute can be set to zero (as opposed to the previous time that was stored).
Easy enough, change the filter to:
filter = "(&(objectClass=user)(lockoutTime>0))"
Dream on - that's not a valid filter query language construct.
So this first list has to be iterated through again to check that the user is actually locked.
Lots of code out there to do this e.g.
or if you want to get fancy - refer c# LDAP check user is locked or notUserPrincipal oUserPrincipal = GetUser(sUserName);userPrincipal.IsAccountLockedOut();
string attribName = "msDS-User-Account-Control-Computed";user.RefreshCache(new string[] { attribName });const int UF_LOCKOUT = 0x0010;int userFlags = (int)user.Properties[attribName].Value;if ( (userFlags & UF_LOCKOUT) == UF_LOCKOUT){// if this is the case, the account is locked outreturn true;}return false;
To unlock - refer Everything in Active Directory via C#.Net 3.5 (Using System.DirectoryServices.AccountManagement).
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.UnlockAccount();
oUserPrincipal.Save();
To unlock via ADUC, click the Account tab on the user's Properties and then check the "Unlock Account" check box. Note: You cannot lock an account either programmatically or through ADUC.
The system will lock the account based on the user's password policy e.g. user will be locked out after x invalid attempts. The policy may state that the user many never be locked out.
Enjoy!
Tuesday, April 09, 2013
AD : Using a filter for UAC bits and null attributes
There’s a lot of functionality in the AD API’s and there are neat API’s to enable / disable users but what if you want to search for them e.g. find all users in an OU that are currently disabled.
You could just search for all the users in an OU and then enumerate through all of them where:
user.Enabled = false;
but that’s a pain.
Enter RFC2254.
Or look here: Search Filter Syntax
What you will find is the matching rule OIDs.
e.g.
1.2.840.113556.1.4.803 = A match is found only if all bits from the attribute match the value. This rule is equivalent to a bitwise AND operator.
UAC is described here: Attributes for AD Users : userAccountControl.
Note that: Const ADS_UF_ACCOUNT_DISABLE = 2.
So now that we have laid the framework, back to the original question.
To search for disabled users, the filter would be:
(userAccountControl:1.2.840.113556.1.4.803:=2)
One of the problems with AD is “null” attributes i.e. attributes that in ADUC display as “not set”.
To search for these e.g. for email, use the filter:
(!(email=*))
* is the AD wildcard so this searches for users who don’t have an email address.
So to search for disabled users who don’t currently have an email address, use:
(&(userAccountControl:1.2.840.113556.1.4.803:=2)(!(email=*)))
This stuff does your head in!
Enjoy!
Monday, November 05, 2012
ADFS : Exclusive Canonicalization transform error
Busy trying to get ADFS v2.0 to work with a third-party SAML implementation and got this weird error:
System.Security.Cryptography.CryptographicException: ID6005: Exclusive Canonicalization transform does not support the algorithm 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'.
“IDxxxx” as an error normally implies it comes from WIF so trawl through that and it turns out that the only transform it accepts is:
http://www.w3.org/2001/10/xml-exc-c14n#
The transforms .NET works with are all listed in:
System.Security.Cryptography.Xml.SignedXml
So I had to get the third party code changed to conform with ADFS.
What is the point of all this, you may ask.
This is all connected with signatures e.g. your sp.xml looks like:
SPSSODescriptor AuthnRequestsSigned="true" WantAssertionsSigned="true" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"
Because XML can have many forms e.g. whitespace or none, CR or CR/LF etc. you need to convert the XML to an agreed format before you sign it. Otherwise the other side may be checking the signature on a “different” document. These “agreed formats” are the canonical transforms. They are all W3C standards.
So e.g. it may state:
The canonical form of an XML document is the physical representation of the document produced by the method described in this specification. The changes are summarized in the following list:
- The document is encoded in UTF-8
- Line breaks normalized to #xA on input, before parsing
- Attribute values are normalized, as if by a validating processor
- Character and parsed entity references are replaced
- CDATA sections are replaced with their character content
- The XML declaration and document type declaration (DTD) are removed
- Empty elements are converted to start-end tag pairs
etc.
Enjoy!
Friday, November 02, 2012
C# : & in the XML
If you have an XML string (e.g. the appSettings in web.config) that contains an “&” it won’t compile e.g.
<add key="URL" value="http://example.com?ID1=abc&ID2=123"/>
The solution is to escape it with the Unicode number i.e.
<add key="URL" value="http://example.com?ID1=abc&ID2=123"/>
Enjoy!
Wednesday, September 05, 2012
Visual Studio : XML comments
Also called programmatic comments or xml documentation.
If you have a method like:
public void blah (String a, int b)
{
}
and you want the comments, just type /// on the line before and you get:
/// <summary>
///
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public void blah (String a, int b)
{
}
If you have ReSharper, put the cursor over the method name i.e. “blah” and press Ctrl / Shift / F1.
You get a neat popup window.
Enjoy!
Friday, August 24, 2012
ASP.NET : reCaptcha with proxy
If you need reCaptcha in your project, this is a really good one to use. it’s also available as a NuGet package.
The only problem is that it doesn’t use a proxy. There is a Proxy attribute in the control but it inherits from IWebProxy and I couldn’t figure out how to set it.
So (with the help of TortoiseSVN) I grabbed the source and added this code to
public RecaptchaResponse Validate() in RecaptchaValidator.cs
//if (this.proxy != null)
//{
//request.Proxy = this.proxy;
IWebProxy rProxy = WebRequest.GetSystemWebProxy();
string login = ConfigurationManager.AppSettings["reCaptchaLogin"];
string password = ConfigurationManager.AppSettings["reCaptchaPassword"];
if (!String.IsNullOrEmpty(login))
rProxy.Credentials = new NetworkCredential(login, password);
else
rProxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = rProxy;
//}
and then I added an appsettings to the web.config in the Tests directory
<appSettings>
<!-- Needed to get past proxy -->
<add key="reCaptchaLogin" value="" />
<add key="reCaptchaPassword" value="" />
</appSettings>
Problem solved!
Enjoy!