【问题标题】:User.Identity.Name is null after authenticate via WIFUser.Identity.Name 通过 WIFI 认证后为空
【发布时间】:2013-02-14 10:45:30
【问题描述】:

我正在使用 WIF 登录我的应用程序。一切似乎都很好(日志记录、重定向到站点等),但是当我尝试在我的 cod 中使用User.Identity.Name 时,异常被抛出-User 为空。任何想法我做错了什么?我在 VS 2012 上工作。web.config 中生成的部分如下所示:

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

和:

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

【问题讨论】:

    标签: asp.net-mvc-3 wif


    【解决方案1】:

    我使用的是:

    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;
            }
        }
    }
    

    并在此上下文中使用
    ((ClaimsIdentity)Thread.CurrentPrincipal.Identity).getName()

    【讨论】:

      【解决方案2】:

      检查 STS 是否包含用户的 Name 声明,否则 User.Identity.Name 将为空。

      【讨论】:

        【解决方案3】:

        使用 WIF 时,您应该使用 Thread.CurrentPrincipal.Identity.Name 而不是 User.Identity.Name。

        在此处阅读更多信息:http://msdn.microsoft.com/en-us/magazine/ff872350.aspx 以了解有关 Windows Identity Foundation 的更多信息

        【讨论】:

          猜你喜欢
          • 2015-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多