【问题标题】:WsFederation authentication in Service Fabric Owin Pipeline not workingService Fabric Owin Pipeline 中的 WsFederation 身份验证不起作用
【发布时间】:2016-12-18 13:22:01
【问题描述】:

生日快乐!

我没有看到太多这方面的内容,因为在撰写本文时它都是全新的。我正在尝试编写一个服务结构应用程序,在用户通过 ACS 进行身份验证后为 Web 应用程序 (html/js) 提供服务。我可以很容易地让它在非服务结构环境中与 OWIN 一起使用,即 IIS 后面的传统 Asp Net 应用程序。我正在尝试将令牌身份验证与 Azure 访问控制结合使用。

所以我现在使用服务结构改变了 OWIN 的工作方式这一事实是否与此有关?下面是我的 Service Fabric 应用程序的 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);
    }

注意我是如何在 Web api 中间件之前注入 WsFederation 中间件的,该中间件最终将用于为我的浏览器 html/js 应用程序提供服务。现在,当它启动并且我进行健全性测试(例如导航到 REST url)时,我的内容会立即提供,而不是被重定向到 Azure 访问控制以登录并获取身份验证令牌。在具有相同 OWIN 配置的传统 Asp Net 应用程序中,我确实在提供任何资源之前被重定向到 Azure 访问控制。

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

任何帮助将不胜感激,感谢您的宝贵时间!

【问题讨论】:

    标签: c# asp.net azure authentication ws-federation


    【解决方案1】:

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

    This article给出教程。

    基本上,在您的代码中,您并没有告诉它进行身份验证。您正在设置所有内容,但您没有启动它。

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

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

    结论:添加登录、注销和应用程序。运行 sn-ps,如果需要,请对其进行最后的调整,应该就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2020-03-19
      • 2014-04-17
      • 2017-02-01
      • 2019-05-15
      • 1970-01-01
      相关资源
      最近更新 更多