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

Gri*_*kin 5 asp.net-mvc adfs ws-federation owin katana

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

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

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

Cli*_*ner 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);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)