MVC5 OWIN ws-federation AuthenticationManager.GetExternalLoginInfoAsync()返回null

时间:2014-12-16 14:28:49

标签: asp.net-mvc owin adfs katana ws-federation

我正在尝试在Visual Studio 2013中的新MVC 5项目中设置集成的OWIN WS-Federation(ADFS)身份验证.Infupup.Auth中的WsFederation配置如下:

app.UseWsFederationAuthentication(wtrealm: "MyRealm",
               metadataAddress: "https://myADFSInstanceHost/FederationMetadata/2007-06/FederationMetadata.xml");  

登录页面上的联合按钮正常工作。 ADFS登录页面是可以实现的,我可以登录。 似乎正确设置了所需的cookie。至少有传递.AspNet.ExternalCookie cookie。 但是当执行回调到mvc app时,在ExternalLoginCallback控制器中,AuthenticationManager.GetExternalLoginInfoAsync()返回null。

1 个答案:

答案 0 :(得分:0)

我知道这是一篇非常老的文章,但是我已经在这个问题上工作了一个星期,这是我发现的唯一可以提供任何帮助的资源。

原始帖子上的评论正是我所需要的。为了使GetExternalLoginInfo工作,必须提出NameIdentifier类型的声明。我可以使用以下代码在Startup.Auth.cs中模拟其中之一:

app.UserWsFederationAuthentication(
    new WsFederationAuthenticationOptions
    {
        Wtrealm = realm, //defined earlier
        MetadataAddress = adfsMetadata, //also defined earlier

        Notifications = new WsFederationAuthenticationNotifications()
        {
            SecurityTokenValidated = notification =>
            {
                ClaimsIdentity identity = notification.AuthenticationTicket.Identity;

                //loop through all the claims returned (this should return everything set up in ADFS)
                foreach (var claim in notification.AuthenticationTicket.Identity.Claims)
                {
                    if (claim.Type == ClaimTypes.Upn) //or whatever claim type you want to use as your name identifier
                    {
                        //This line will add a duplicate claim, giving it the specified type. This NEEDS TO BE `NameIdentifier`
                        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, claim.Value));
                    }
                }
                return Task.FromResult(0);
            }
        }
    });