Service Fabric Owin Pipeline中的WsFederation身份验证无法正常工作

Mil*_*Mil 6 c# asp.net authentication azure ws-federation

天儿真好!

我在这方面没有看到太多,因为在写这篇文章时它非常新颖.我正在尝试编写一个服务结构应用程序,在用户通过ACS进行身份验证后,它将为Web应用程序(html/js)提供服务.我可以轻松地在非服务结构环境中使用OWIN,即IIS后面的传统Asp Net应用程序.我正在尝试使用Azure Access Control进行令牌身份验证.

那么与我现在使用服务结构的事实有什么关系改变了OWIN的工作方式?下面是我的服务结构应用程序中的Startup.cs中的OWIN ConfigureApp()函数:

public static void ConfigureApp(IAppBuilder appBuilder)
    {                           
        appBuilder.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions());

        appBuilder.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                Wtrealm = _realm,
                MetadataAddress = _acsXmlMetaDataUrl
            });

        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        appBuilder.UseWebApi(config);
    }
Run Code Online (Sandbox Code Playgroud)

请注意我是如何最终用于为我的浏览器html/js应用程序提供服务的web api 中间件之前注入WsFederation中间件的.现在,当这个启动并且我进行健全性测试时,例如导航到REST URL,我的内容会立即提供,而不是重定向到Azure Access Control以登录并获取身份验证令牌.在我使用相同OWIN配置的传统Asp Net应用程序中,我确实在提供任何资源之前将其重定向到Azure Access Control.

所以我的问题是如何将WsFed中间件注入OWIN管道,以便在服务结构上下文中工作?

非常感谢任何帮助,谢谢你的时间!

小智 1

我不知道为什么这段代码适用于 MVC 而不适用于 Service Fabric。我也遇到了同样的问题,但我找到了一种方法让它适用于 SF。

本文给出了一个教程。

基本上,在您的代码中,您没有告诉它进行验证。你正在设置一切,但你还没有开始它。

app.Map("/login", map =>
            {
                map.Run(async ctx =>
                {
                    if (ctx.Authentication.User == null ||
                        !ctx.Authentication.User.Identity.IsAuthenticated)
                    {
                        ctx.Response.StatusCode = 401;
                    }
                    else
                    {
                        ctx.Response.Redirect("/");
                    }
                });
            });

app.Run(async ctx =>
            {
                var user = ctx.Authentication.User;
                var response = ctx.Response;

                response.ContentType = "text/html";

                if (user != null && user.Identity.IsAuthenticated)
                {
                    await response.WriteAsync(string.Format("<h2>{0}</h2>",
                        user.Claims.First().Issuer));

                    await response.WriteAsync("<dl>");
                    foreach (var claim in user.Claims)
                    {
                        await response.WriteAsync(string.Format(
                            "<dt>{0}</dt> <dd>{1}</dd>",
                            claim.Type,
                            claim.Value));
                    }
                    await response.WriteAsync("</dl>");
                }
                else
                {
                    await ctx.Response.WriteAsync("<h2>anonymous</h2>");
                }
            });
Run Code Online (Sandbox Code Playgroud)

当您访问网站上的链接时,app.Run 中的代码开始执行以检查您是否已登录。如果您没有登录,在这种情况下,它将在页面上写入“匿名”而不是加载你的内容。要进行身份验证,请转到您的网站/登录,它会将您重定向到您在配置中拥有的任何身份验证提供程序。

结论:添加登录、注销和 app.Run 片段,如果需要的话,进行最后的调整,就这样了。