bun*_*985 10 .net asp.net-mvc adfs claims-based-identity claims
我在使用.Net MVC 5 app配置ADFS时遇到问题.
我已经在VS 2015中配置我的项目以使用声明并且它工作正常,但我有一个问题.
我可以登录,使用ADFS,我可以检查用户角色等.当我尝试使用时出现问题
[Authorize(Roles="somenonExistingRole")]
Run Code Online (Sandbox Code Playgroud)
尽管我已经通过身份验证,但是当重新进行身份验证时,我被重定向到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();
}
}
Run Code Online (Sandbox Code Playgroud)
和配置身份验证部分
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = realm,
MetadataAddress = adfsMetadata,
});
}
Run Code Online (Sandbox Code Playgroud)
Tho*_*mas 14
要修复循环问题,您应该覆盖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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3557 次 |
最近记录: |