【问题标题】:Redirect loop with .Net MVC Authorize attribute with ADFS Claims带有 ADFS 声明的 .Net MVC 授权属性的重定向循环
【发布时间】:2016-04-28 18:26:21
【问题描述】:

我在使用 .Net MVC 5 应用程序配置 ADFS 时遇到问题。

我已在 VS 2015 中将我的项目配置为使用声明,它工作正常,但我有一个问题。

我可以登录,使用 ADFS,我可以检查用户角色等。当我尝试使用时出现问题

[Authorize(Roles="somenonExistingRole")]

尽管我已经通过身份验证,但当再次进行身份验证时,我被重定向到 ADFS 页面,并且我被重定向到发生循环的页面。页面将我发送到 ADFS 门户,ADFS 将我重定向到门户,经过几次尝试后,我从 ADFS 收到错误(对许多请求)

我必须自己实现角色提供者之类的东西吗?或者我需要配置一些额外的东西。也许我可以限制尝试次数?为什么我已经准备好角色后会重定向到 ADFS?

在代码中实际上并没有太多可显示的内容,而是按要求显示: 我正在测试的控制器:

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [Authorize]
        public ActionResult About()
        {
            var u = HttpContext.User;


            if (u.IsInRole("/"))
            {
                ViewBag.Message = "User is in role.";
            }
            else
            {
                ViewBag.Message = "User is NOT in role.";
            }

            return View();
        }
        [Authorize(Roles = "/nonexistingRole")]
        public ActionResult Contact()
        {

            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

和配置身份验证部分

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions());

    app.UseWsFederationAuthentication(
        new WsFederationAuthenticationOptions
        {
            Wtrealm = realm,
            MetadataAddress = adfsMetadata, 

        });
}

【问题讨论】:

  • 你的 AuthenticateAttribute 是什么?你能告诉我们代码吗
  • sory - 凭记忆写 授权offcourse :)
  • 你有配置 authent 的 Startup.Auth 类吗?
  • 你的意思是部分类 Startup 和 ConfigureAuth 功能?如上图
  • 您能否更新帖子的标题以进一步解释您的问题,例如:Redirect loop with .Net MVC Authorize attribute with ADFS Claims?欢呼

标签: .net asp.net-mvc claims-based-identity adfs claims


【解决方案1】:

要解决循环问题,您应该覆盖AuthorizeAttribute

默认情况下,当用户的角色不满足AuthorizeAttribute 要求时,MVC 会返回 401 Unauthorized。这会初始化对身份提供者的重新认证请求。由于用户已经登录,AAD 返回到同一页面,然后发出另一个 401,创建重定向循环。在这里,我们重写了 AuthorizeAttribute 的 HandleUnauthorizedRequest 方法,以显示在我们的应用程序上下文中有意义的内容。

这个类是在使用 VS 2015 创建新的 MVC 项目时生成的:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{        
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAuthenticated)
        {
            //One Strategy:
            //filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);

            //Another Strategy:
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary(
                    new
                    {
                        controller = "Error",
                        action = "ShowError",
                        errorMessage = "You do not have sufficient priviliges to view this page."
                    })
                );
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}

【讨论】:

  • 但是当用户有角色时它可以工作。仅当用户不在角色中时才会出现此问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
相关资源
最近更新 更多