AD FS Compatibility

To get compatibility with AD FS I needed to make the following changes.
The commits should be self explanatory, so I won't explain any change here in detail.