Search code examples
asp.net-mvc-3wif

User.Identity.Name is null after authenticate via WIF


I'm using WIF to log in my appication. Everything seems to be ok (logging,redirecting to site etc),but when i try use User.Identity.Name in my cod exception is being thrown-User is null.Any ideas what i'm doing wrong? I work on VS 2012. Generated part in web.config looks like below:

  <system.identityModel>
    <identityConfiguration>
      <audienceUris>
        <add value="http://xxx/" />
      </audienceUris>     
      <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <trustedIssuers>
          <add thumbprint="yyyy" name="https://zzz" />
        </trustedIssuers>
      </issuerNameRegistry>
    </identityConfiguration>
  </system.identityModel>

and:

  <system.identityModel.services>
    <federationConfiguration>
      <cookieHandler requireSsl="false" />
      <wsFederation passiveRedirectEnabled="true" issuer="https://zzz/Secure/FederatedLogin.ashx" realm="http://xxx" requireHttps="false" />          
    </federationConfiguration>        
  </system.identityModel.services>

Solution

  • When working with WIF you should use Thread.CurrentPrincipal.Identity.Name instead of User.Identity.Name.

    Read more here: http://msdn.microsoft.com/en-us/magazine/ff872350.aspx to learn more about Windows Identity Foundation