2015-02-11 139 views
0

我創建了一個MVC 5項目並使用Windows身份驗證。 我的問題是每當用戶的操作被操作屬性[Authorize(Roles =「Roelabc」)]拒絕時,瀏覽器將彈出一個包含用戶名/密碼的登錄提醒。 現在我不希望這個彈出,我只是想如果用戶被拒絕,然後重定向用戶到自定義頁面。 非常感謝。MVC 5 Windows身份驗證重定向

回答

0

創建派生自AuthorizeAttribute的類,將解決我的問題。

public class WindowsAuthorize : AuthorizeAttribute 
    { 
     public override void OnAuthorization(AuthorizationContext filterContext) 
     { 
      base.OnAuthorization(filterContext); 

      if (filterContext.Result is HttpUnauthorizedResult) 
      { 
       filterContext.Result = new RedirectToRouteResult(
        new RouteValueDictionary 
       { 
        { "client", filterContext.RouteData.Values[ "client" ] }, 
        { "controller", "Home" }, 
        { "action", "Contact" }, 
        { "ReturnUrl", filterContext.HttpContext.Request.RawUrl } 
       }); 
      } 
     } 
    } 

而且使用這樣的

[WindowsAuthorize(Roles = "User1")] 
[HttpGet] 
public ActionResult Index(string runDate = "") 
{ ... } 
0

您可以創建一個名爲unathorized的視圖。而在你的登錄類中,只是重定向到該視圖。你可以在logon.cs中使用這樣的東西

if (AuthenticateUser()) 
     { 
      if (CheckRulesOfBehavior()) 
      { 
       AuthorizeUser(); 
       HttpContext.Response.Redirect("~/Work/Index"); 
      } 
    else 
     { 
      HttpContext.Response.Redirect("~/Errors/Unauthorized"); 
     } 
+0

嗨,有在Windows身份驗證沒有登錄功能。非常感謝。我通過覆蓋默認的Authorize屬性找到了解決方案。見下面的帖子。 – shaun2099 2015-02-12 01:16:46