【问题标题】:OWIN WsFederation authentication with in-app authorization具有应用内授权的 OWIN WsFederation 身份验证
【发布时间】:2017-01-06 23:40:45
【问题描述】:

场景:

  • 公司内部用户的 ASP.NET MVC Web 应用程序,配置为针对公司的 ADFS 进行身份验证,使用 Microsoft.Owin.Security.WsFederation
  • 公司的 ADFS 包含多个用户,但应该只有少数用户能够登录到应用程序。
  • 因此我有一个包含这些用户电子邮件地址的数据库表
  • Web 应用程序应检查从 ADFS 收到的电子邮件声明是否存在于 DB 表中,并且仅为这些用户发出登录令牌。
  • 应将其他用户重定向到“抱歉,您无权使用此应用程序”页面。

我的问题:

  • 在哪里放置用于检查是否应允许用户进入的授权逻辑的正确位置?

这是我的 Startup.Configuration 方法中的代码:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(
    new WsFederationAuthenticationOptions
    {
        MetadataAddress = "https://.../FederationMetadata.xml",
        Wtrealm = "...",                        
    }
);

【问题讨论】:

  • 你好,你能解决这个问题吗?我有同样的问题,我想根据 adfs 的声明检查数据库。
  • 不,我没弄明白。
  • 你好@codeape,我想通了,我写了一篇关于它的小博文,可能对你有帮助:durgha.com/…
  • @TheWebGuy:从您的博客文章中,我无法弄清楚如果登录用户未获得授权,您将如何进行重定向。您是否应该以某种方式使用通知参数来操纵响应?或者你应该返回一个执行它的任务?
  • 我不知道如何从通知中进行重定向,从技术上讲,在创建 cookie 后会调用“SecurityTokenValidated”,所以我做了一个 hack。我创建了一个新角色(例如:NotValidUser),然后我创建了一个自定义授权属性来检查并重定向该声明是否存在。如果您找到更简单的方法,请告诉我。我真的对那个解决方案不满意。

标签: asp.net-mvc authentication authorization owin ws-federation


【解决方案1】:

您有两种选择来实现您想要的:

1. 一种是在 AD FS 上进行配置。我个人认为这是正确的做法,因为 AD FS 是 IdP,它应该是控制其用户是否有权访问应用程序的一个。在这种情况下,公司应该或不应该允许某人使用其部分资源(当然也有反对意见)。这可以通过 AD FS 管理 GUI 在域控制器上轻松完成。以下答案很好地描述了这一点: https://serverfault.com/a/676930/321380

2.二是在OWIN WSFed中间件中使用Notifications对象,如下:

            Notifications = new WsFederationAuthenticationNotifications()
            {
                SecurityTokenValidated = (context) =>
                {
                    //extract claims' values and check identity data against your own authorization logic
                    bool isAuthorized = CheckForUnauthorizedAccess();
                    if (!isAuthorized)
                    {
                        throw new SecurityTokenValidationException("Unauthorized access attemp by {some_identifier}");
                    }
                    return Task.FromResult(0);
                },
                AuthenticationFailed = (context) =>
                {
                    if (context.Exception is an unauthorized exception)
                    {
                        context.OwinContext.Response.Redirect("<unauthorized_redirect_url>");
                    }
                    context.HandleResponse(); // Suppress the exception
                    //exception logging goes here
                    return Task.FromResult(0);
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多