Kamil Będkowski
Kamil Będkowski

Reputation: 1092

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>

Upvotes: 3

Views: 4564

Answers (3)

charlierlee
charlierlee

Reputation: 300

Instead I used:

namespace System.Security.Claims
{
    public static class System_Security_Claims_Extensions
    {
        public static string getName(this ClaimsIdentity ci)
        {
            foreach (Claim c in ci.Claims)
            {
                if (c.Type == ClaimTypes.Name)
                {
                    return c.Value;
                }
            }
            return string.Empty;
        }
    }
}

And used in this context
((ClaimsIdentity)Thread.CurrentPrincipal.Identity).getName()

Upvotes: 0

klings
klings

Reputation: 973

Check that the STS includes a Name claim for the user, else User.Identity.Name will be null.

Upvotes: 2

Kacper Ryniec
Kacper Ryniec

Reputation: 93

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

Upvotes: 2

Related Questions