Azure Active Directory and WCF authentication RRS feed

  • Question

  • I have WCF service and I need to secure it with Azure Active Directory. I've already read all related questions here and at social.msdn but still can't get my sample working. I want authentication to work in following way.

    1. No any popups displayed when user calls WCF service from client.
    2. User passes username/password and receives auth token from Azure ADFS
    3. Then in application (let it be console app for now) we open channel via Create CreateChannelWithIssuedToken method and all subsequent calls are made with the token we get from AAD.

    What I'm doing and what my problems are. I'm for now using VS 2012 and Identity and Access tool to generate proper config file for the WCF service. So my service has following address. http://localhost:1785/Service1.svc

    I go to azure portal and create new Web application with name http://localhost:1785/Service1.svc, sign on url http://localhost:1785/Service1.svc and app id url http://localhost:1785/Service1.svc

    Then I check endpoints of the application and copy federation metadata which looks as https://login.windows.net/{some guid}/federationmetadata/2007-06/federationmetadata.xml

    Using Identity and Access tools I add WAAD Identity provider to the Service project. My web.config file looks as below:

    <?xml version="1.0"?>
        <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
        <add key="ida:FederationMetadataLocation" value="https://login.windows.net/34bb8966-5537-4b1b-85ed-f501a06c1225/federationmetadata/2007-06/federationmetadata.xml" />
        <add key="ida:ProviderSelection" value="productionSTS" />
      <location path="FederationMetadata">
            <allow users="*" />
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false" />
              <serviceCredentials useIdentityConfiguration="true">
                <!--Certificate added by Identity and Access Tool for Visual Studio.-->
                <serviceCertificate findValue="CN=localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectDistinguishedName" />
          <add scheme="http" binding="ws2007FederationHttpBinding" />
          <add binding="basicHttpsBinding" scheme="https" />
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
            <binding name="">
              <security mode="Message">
                  <issuerMetadata address="https://login.windows.net/adfs/services/trust/mex" />
        <modules runAllManagedModulesForAllRequests="true" />
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
        <directoryBrowse enabled="true" />
            <add value="http://localhost:1785/Service1.svc" />
          <issuerNameRegistry type="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry, System.IdentityModel.Tokens.ValidatingIssuerNameRegistry">
            <authority name="https://sts.windows.net/34bb8966-5537-4b1b-85ed-f501a06c1225/">
                <add thumbprint="92B88C3DD981BF1EBCB244FCFA63C007706C79E0" />
                <add thumbprint="3270BF5597004DF339A4E62224731B6BD82810A6" />
                <add name="https://sts.windows.net/34bb8966-5537-4b1b-85ed-f501a06c1225/" />
          <!--certificationValidationMode set to "None" by the the Identity and Access Tool for Visual Studio. For development purposes.-->
          <certificateValidation certificateValidationMode="None" />

    Then I created console application and added service reference to the service project. After adding reference, config looks as

    <?xml version="1.0" encoding="utf-8" ?>
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
                    <binding name="WS2007FederationHttpBinding_IService1">
                                <!--<issuer address="http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous" />-->
                              <issuer address="https://login.windows.net/34bb8966-5537-4b1b-85ed-f501a06c1225/wsfed" binding="ws2007HttpBinding"/>
                              <issuerMetadata address="https://login.windows.net/adfs/services/trust/mex" />
                                    <trust:SecondaryParameters xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
                                        <trust:KeyType xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</trust:KeyType>
                                        <trust:KeySize xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">256</trust:KeySize>
                                        <trust:KeyWrapAlgorithm xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p</trust:KeyWrapAlgorithm>
                                        <trust:EncryptWith xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2001/04/xmlenc#aes256-cbc</trust:EncryptWith>
                                        <trust:SignWith xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2000/09/xmldsig#hmac-sha1</trust:SignWith>
                                        <trust:CanonicalizationAlgorithm xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2001/10/xml-exc-c14n#</trust:CanonicalizationAlgorithm>
                                        <trust:EncryptionAlgorithm xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2001/04/xmlenc#aes256-cbc</trust:EncryptionAlgorithm>
                    <binding name="">
                        <security mode="TransportWithMessageCredential">
                            <transport clientCredentialType="InheritedFromHost" />
                            <message establishSecurityContext="false" />
                <endpoint address="http://localhost:1785/Service1.svc" binding="ws2007FederationHttpBinding"
                    contract="SomeService.IService1" name="WS2007FederationHttpBinding_IService1">

    I tried to launch console application, but the error that valid issue should be specified has raised. So I thought, that valid issue can be found in endpoints at Azure AD configuration page, and copied WS-Federation Single sign-on endpoint as an issuer.

    Console program code is below.

    var client = new SomeService.Service1Client();
                client.ClientCredentials.UserName.UserName = "<valid user name>";
                client.ClientCredentials.UserName.Password = "<password>";

    When I call service, I see in fiddler, that request is sent to WS-Fed endpoint but in response endpoint sends me html page with error

    AADSTS20012: An error occurred when we tried to process a WS-Federation message. The message was invalid.

    Could you please suggest me where I'm wrong and what to do to implement scenario I described in the beginning of the question? Thanks.

    Friday, November 14, 2014 1:08 PM


All replies

  • Hi Oleg,

    Thanks for posting here!

    AADSTS20012 - With the error message provided above I presume that it could be an issue with the portal as mentioned in this thread link.

    Just to make sure you have followed the steps correctly and there is no miss on it, refer this link - http://blogs.msdn.com/b/brunoterkaly/archive/2014/04/01/exercise-3-securing-a-wcf-service-using-windows-azure-active-directory.aspx

    You might also want to check this reference link - http://msdn.microsoft.com/en-us/library/ff647503.aspx

    A similar scenario reference - http://stackoverflow.com/questions/16403426/azure-wcf-service-with-azure-active-directory-authentication

    Hope this helps!

    Let me know if you have any queries!

    Best Regards,

    Sadiqh Ahmed

    Disclaimer: This response contains a reference to a third party World Wide Web site.
    Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Friday, November 14, 2014 4:21 PM
  • Thank you, Sadiqh.

    I've already visited all the links you suggested, before posting to the forum.

    Let me clarify the question a little.

    I'm not sure, that I use correct issuer address. Because one that is generated for our local ADFS service is

    https://<adfs address>/adfs/services/trust/13/usernamemixed.

    Could you please either confirm that


    is correct as issuer address, or point me to valid address for Azure Active Directory ADFS.


    Friday, November 14, 2014 4:39 PM
  • Hi Oleg,

    We are looking into this issue. Would require sometime to assist you with the right answer.

    We shall update this thread with our findings shortly.


    Sadiqh Ahmed

    Monday, November 17, 2014 8:02 PM
  • Sadiqh, thanks a lot. Any help would be appreciated.

    Meanwhile, I have another question that is related to the topic, so I'm asking it here.

    Does Azure Active Directory ADFS support active client authorization?

    I mean is it possible to get auth token just by passing login/password to Azure ADFS in any way, without opening browser and switching to passive mode?


    Tuesday, November 18, 2014 11:00 AM
  • Unfortunately, WCF uses WS-Trust which is not supported by Azure AD.
    Friday, November 21, 2014 10:51 PM
  • I'm marking the above post as answer. Please write back or create a new forum post if you need further assistance.


    Sadiqh Ahmed

    Friday, November 28, 2014 7:23 PM