User.Identity.Name is null after authenticate via WIF

怎甘沉沦 提交于 2019-12-14 01:15:52

问题


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>

回答1:


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




回答2:


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




回答3:


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()



来源:https://stackoverflow.com/questions/15132220/user-identity-name-is-null-after-authenticate-via-wif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!