2017-06-27 2 views
0

Meine Frage:Wie Fenster Sicherheitsfenster Aufforderung zur Autorisierung lösen scheiterte in mvc

  1. Wenn der Benutzer nicht-Manager-Rolle und Admin-Rolle haben, muss ich umleiten Seite/eine Popup-Meldung auf Fehler. Aber wenn ich überprüft habe, dass "falsch" fortwährend Windows-Sicherheits-Passwort-Fenster autorisieren, zeigt es. Wenn ich Benutzernamen und Passwort erneut eingegeben habe, wird das Windows-Sicherheitskennwort angezeigt.

  2. Jede Aktionsmethode, die ich überprüfen muss, und ich muss die Nachricht oder die Fehlerseite anzeigen. Wie löst man diese Probleme?

Controller-Code:

[AuthorizeUser("Manager","Admin")] 
public ActionResult Contact() 
{ 
    return View();  
} 

C# -Code:

public AuthorizeUserAttribute(params int[] roles) 
{ 
    allowedroles = roles; 
} 

protected override bool AuthorizeCore(HttpContextBase httpContext) 
{ 
    bool authorize = false; 
    var getList = _objService.GetUserRoleDetail(CommonStaticHelper.getLoggedUser()); 

    foreach (var role in allowedroles) 
    { 
     if (getList.Exists(m => m.RoleId == role)) 
     { 
      return authorize = true; /* return true if Entity has current user(active) with specific role */ 
     } 
    } 
    return authorize; 
} 

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
{ 
    filterContext.Result = new HttpUnauthorizedResult(); 
} 

Antwort

2

/// Try this:

///Create an action : 

     public ActionResult Unauthorized() 
       { 
        return View(); 
       }  
//// now write below code for authorization   


    protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext) 
       { 

        if (filterContext.HttpContext.Request.IsAuthenticated) 
        { 
         //redirect to the Unauthenticated page 
         filterContext.Result = new RedirectToRouteResult(new 
RouteValueDictionary(new { controller = "Error", action = "Unauthorized" 
})); 
        } 
        else 
        { 
         base.HandleUnauthorizedRequest(filterContext); 
        } 
       } 



       protected override bool AuthorizeCore(HttpContextBase httpContext) 
       { 
        var authorized = base.AuthorizeCore(httpContext); 


        if (!authorized) 
        { 
         // The user is not authenticated 
         return false; 
        } 
        else{ 
     var getList = 
     _objService.GetUserRoleDetail(CommonStaticHelper.getLoggedUser()); 

      foreach (var role in allowedroles) 
      { 
       if (getList.Exists(m => m.RoleId == role)) 
       { 
        return authorize = true; /* return true if Entity has current 
        user(active) with specific role */ 
       } 
      } 

       return authorize = false; 

       } 
+0

seine Arbeits .. Dank. – SENA

0

Ihre eigenen Filter so etwas wie

public class AuthorityAttribute : AuthorizeAttribute 
    { 
     private readonly string[] allowedroles; 
     public AuthorityAttribute(params string[] roles) 
     { 
      this.allowedroles = roles; 
     } 
     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      foreach (var role in allowedroles) 
      { 
       if (PortalWebSessionManager.ActivePortalSettings.ActiveRoles != null) 
       { 
        foreach (IDynamics.IDynamicsPortal.DataComponent.Roles currentRole in PortalWebSessionManager.ActivePortalSettings.ActiveRoles) 
        { 
         if (currentRole.RoleName == role) 
         { 
          return true; 
         } 
        } 
       } 
      } 
      return false; 
     } 
     protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
     { 
      filterContext.Result = new HttpUnauthorizedResult(); 
     } 
    } 

erstellen und rufen Sie diesen Filter

+0

lass mich es versuchen. Danke – SENA

Verwandte Themen