2013-09-06 31 views
10

Tôi đang làm việc trên một trang web đơn giản trong asp.net. Tôi muốn hạn chế quyền truy cập vào một bên, do đó chỉ cho phép người dùng trong một nhóm AD cụ thể. Tôi đã làm điều đó và nó hoạt động tốt. Nhưng khi người dùng không có trong nhóm AD cố truy cập trang web, họ sẽ nhận được lời nhắc đăng nhập. Làm cách nào để chuyển hướng người dùng trái phép đến trang tùy chỉnh, thay vì họ nhận được lời nhắc đăng nhập?Chuyển hướng người dùng trái phép asp net

Dưới đây là web.config của tôi. Phần thấp nhất của mã, là một cái gì đó tôi đã cố gắng nhưng không hoạt động.

<configuration> 
<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="Windows"/> 
    <authorization> 
    <allow roles="DOMAIN\GROUP"/> 
    <deny users="*"/> 
    </authorization> 
</system.web> 

<location path="AccessDenied.aspx"> 
<system.web> 
<authorization> 
    <allow users="*"/> 
</authorization> 
</system.web> 
</location> 
</configuration> 

Tôi đã thêm này đến Global.asax.cs:

protected void Application_EndRequest(Object sender, EventArgs e) 
    { 
     if (HttpContext.Current.Response.Status.StartsWith("401")) 
      { 
       HttpContext.Current.Response.ClearContent(); 
       Server.Execute("AccessDenied.aspx"); 
      } 
} 

Bất kỳ ý tưởng?

EDIT: Tôi đã thử một số giải pháp đã đăng, nhưng chúng không hoạt động. Nhưng tôi nhận nó làm việc với mã này:

void Application_EndRequest(object sender, System.EventArgs e) 
    { 
     if (((Response.StatusCode == 401) 
     && (Request.IsAuthenticated == true))) 
     { 
      Response.ClearContent(); 
      Response.Redirect("~/AccessDenied.aspx"); 
     } 
    } 
} 
+0

vì '' nghĩa là bất kỳ người dùng được phép truy cập, bạn phải cho phép một nhóm hoặc người dùng cụ thể. –

+0

Có, vì một số lý do một số web.config không được hiển thị. – mads

+0

'Propmt' có nghĩa là trang lỗi màu vàng phải không ?? –

Trả lời

3

Bạn có thể sử dụng Response.Redirect hoặc Server.Transfer

Response.Redirect("AccessDenied.aspx"); 

Full dụ:

protected void Application_EndRequest(Object sender, EventArgs e) 
{ 
    if (HttpContext.Current.Response.Status.StartsWith("401")) 
    { 
     HttpContext.Current.Response.ClearContent(); 
     Response.Redirect("AccessDenied.aspx"); 
    } 
} 
2

Giả sử bạn muốn để xử lý tất cả Lỗi "Không được ủy quyền":

<customErrors defaultRedirect="Error.aspx" mode="On"> 
    <error statusCode="401" redirect="Unauthorized.aspx" /> 
    <error statusCode="403" redirect="Forbidden.aspx" /> 
</customErrors> 

Bất kỳ yêu cầu 401 (trái phép) nào sẽ được chuyển tiếp đến Unauthorized.aspx.

+2

Điều này không hoạt động, mặc dù nó phải. Microsoft đôi khi đưa ra quyết định thú vị trong thiết kế của họ. –

+0

Điều này có thể không hoạt động do thực tế là '401 trái phép' được trả về bởi UrlAuthorizationModule được chuyển thành '302 Redirect' bởi FormsAuthenticationModule: http://www.asp.net/web-forms/overview/older-versions- security/membership/user-based-authorization-cs – track0

0
<authorization> 
<!--<allow users="*"/>-->This here means allow everyone . 
<allow users="AD"/> -- Add this group to AD domain . 
<deny users="?"/> --Deny unknown users(Not authenticated) 
<allow roles="Admins"/> --If you have created roles .  

Nếu bạn có nhóm địa phương vì sử dụng <allow user ="AD"> nhưng bạn phải đăng ký nó vào miền AD. <allow roles ="AD" /> sẽ chỉ hoạt động với Nhóm quảng cáo miền không dành cho các nhóm địa phương.

protected void Application_EndRequest(Object sender,EventArgs e) 

    { 
    HttpContext context = HttpContext.Current; 
    if (context.Response.Status.Substring(0,3).Equals("401")) 
    { 
     context.Response.ClearContent(); 
     //do redirect here 
    } 
    } 
+0

'cho phép người dùng' là dành cho tên tài khoản người dùng, không phải cho nhóm quảng cáo. – Nicholas

+0

@Nicholas Nếu bạn chỉ có thể kiểm tra câu lệnh này 'Nếu bạn có nhóm địa phương hơn là sử dụng nhưng bạn phải đăng ký nó vào miền AD. sẽ chỉ hoạt động với các nhóm AD Domain không dành cho các nhóm cục bộ.'Tuyên bố này có thể gây nhầm lẫn và dẫn đầu người đọc theo một hướng khác tôi có thể thay đổi nó với những từ tốt hơn cho đến khi cảm ơn vì đã chỉ ra nhưng tôi không nói người dùng là 'cho phép Active Directory'. –

+0

Nếu bạn sử dụng 'allow roles' và đặt trình quản lý vai trò thành' WindowsTokenRoleProvider', nó sẽ phát hiện cả nhóm máy tính cục bộ và nhóm bảo mật miền nếu máy chủ là thành viên của miền. Xem https://msdn.microsoft.com/en-us/library/wce3kxhd%28v=vs.100%29.aspx – Nicholas

1

tôi đã thành công hơn với điều này:

 // This is a workaround for the fact that we are not using MVC and its attributes 
     // This is the situation where a user is logged in - but not authorized for this page 
     void Application_EndRequest (object sender, System.EventArgs e) 
     { 
     if (((Response.StatusCode == 302) && (Request.IsAuthenticated == true))) 
     { 
      try 
      { 
       string sLoc = Response.Headers ["Location"]; 
       if (sLoc.Contains ("Login")) 
       { 
        Response.ClearContent(); 
        Response.Redirect ("~/AccessDenied.aspx"); 
       } 
      } 
      catch 
      { 
      } 
     } 
     } 
Các vấn đề liên quan