2011-10-11 26 views
9

Các bài viết sau đây cho biết cách thiết lập web.config cho trang web bằng cách sử dụng Xác thực chế độ hỗn hợp. IIS7 Mixed Mode AuthenticationHow to allow mixed-mode authentication in IIS 7.0.Cách cấu hình IIS7 khi sử dụng xác thực chế độ hỗn hợp với asp.net

Tôi đã thiết lập trang web và hoạt động cục bộ (trên máy nhà phát triển của mình). Tuy nhiên, khi tôi chạy nó cục bộ trên máy chủ tôi nhận được 401.2 - Đăng nhập thất bại do lỗi cấu hình máy chủ.

Bất cứ ai biết cách tôi định cấu hình máy chủ, Trang web mặc định và Trang web của tôi?

Chỉnh sửa: Dưới đây là các cài đặt trong web.config của tôi, bao gồm loginUrl từ nút Xác thực biểu mẫu.

<location path="~/Account/WinLogin.aspx"> 
    <system.web> 
     <authorization> 
     <deny users="?"/> 
     <allow users="*"/> 
     </authorization> 
    </system.web> 
    <system.webServer> 
     <security> 
     <authentication> 
      <anonymousAuthentication enabled="false"/> 
      <windowsAuthentication enabled="true"/> 
     </authentication> 
     </security> 
    </system.webServer> 
    </location> 
    <system.web> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/WinLogin.aspx" timeout="60"/> 
    </authentication> 
    <authorization> 
     <deny users="?"/> 
    </authorization> 

Trả lời

11

Hãy bắt đầu với cấu hình role máy chủ (đây là dưới thời HLV máy chủ, vai trò, IIS)

Bạn đang gonna muốn chắc chắn rằng cửa sổ auth và phần auth vô danh được kích hoạt/cài đặt, và cũng là các hình thức auth (mà có lẽ bạn đã có). Sau những được cài đặt/cấu hình, bạn sẽ cần phải xác định những thứ sau:

Trong Web.Config của bạn, bạn sẽ muốn có các phần sau được xác định:

<configuration> 
    <system.web> 
    <authentication mode="Forms"> 
     <forms cookieless="UseDeviceProfile" defaultUrl="~/Default.aspx" enableCrossAppRedirects="true" loginUrl="~/WindowsLogin.aspx" name=".ASPXAUTH" path="/" protection="All" requireSSL="false" slidingExpiration="true" timeout="10080"/> 
    </authentication> 
    <authorization> 
     <deny users="?"/> 
    </authorization> 
    </system.web> 
    <location path="Login.aspx"> 
     <system.web> 
      <authorization> 
       <allow users="?"/> 
      </authorization> 
     </system.web> 
     <system.webServer> 
      <security> 
       <authentication> 
        <anonymousAuthentication enabled="true"/> 
        <windowsAuthentication enabled="false"/> 
       </authentication> 
      </security> 
     </system.webServer> 
    </location> 
    <location path="WindowsLogin.aspx"> 
     <system.web> 
      <authorization> 
       <deny users="?"/> 
       <allow users="*"/> 
      </authorization> 
     </system.web> 
     <system.webServer> 
      <security> 
       <authentication> 
        <anonymousAuthentication enabled="false"/> 
        <windowsAuthentication enabled="true"/> 
       </authentication> 
      </security> 
     </system.webServer> 
    </location> 
</configuration> 

Sau đó, bạn sẽ cần hai tập tin:

Login.aspx (this does forms auth) 
WindowsLogin.aspx (this does Windows auth) 

ĐĂNG NHẬP làm hình thức, phải, do đó chỉ là bog tiêu chuẩn ASP.NET tạo auth đó là WindowsLogin mà không sự kỳ diệu (và đây là tập tin đó)

using System; 
using System.Web; 
using System.Web.Security; 
using App_Code.Biz; 

public partial class WindowsLogin : System.Web.UI.Page { 
    protected string UserIsInRoles = string.Empty; 
    private static readonly BAL _mBAL = new BAL(); 
    protected void Page_Load(object sender, EventArgs e) { 
     string redirectUrl = Request["returnurl"] ?? "~/default.aspx"; 
     string username = Request.ServerVariables["LOGON_USER"]; 
     try { 
      if (Roles.GetRolesForUser(username).Length < 1) 
       Roles.AddUserToRole(username, Global.defaultRole); 
      int status; 
      _mBAL.aspnet_Membership_CreateUser(username, out status); 
     } catch (Exception ex) { 
      ErrHandler.WriteXML(ex); 
     } 

     /* Test to see if the user is in any roles */ 
     if (Roles.GetRolesForUser(username).Length < 1) { 
      UserIsInRoles = "<br />" + username + "You are not in any rules. This must be your first visit to our site!<br /> Adding you to the " + Global.defaultRole + " role now!"; 

     } else { 
      UserIsInRoles = "You are in the following roles: "; 
      string[] roles = Roles.GetRolesForUser(username); 
      foreach (string role in roles) 
       UserIsInRoles += role + ", "; 
      UserIsInRoles = UserIsInRoles.Remove(UserIsInRoles.Length - 2) + "!"; 

      if (Login(username, String.Join(",", roles))) 
       Response.Redirect(redirectUrl); 
     } 

     //we shouldn't get here, so if we do, redirect back to a page they can use. 
     if (Page.IsPostBack) { 
      if (Response.StatusCode == 401) 
       Response.Redirect("~/Login.aspx"); 

     } 
    } 

    private bool Login(string strUser, string strRole) { 
     if (strRole != null) { 
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
       1,       // version 
       strUser,      // user name 
       DateTime.Now,     // create time 
       DateTime.Now.AddYears(1),  // expire time 
       false,      // persistent 
       strRole);      // user data 
      string strEncryptedTicket = FormsAuthentication.Encrypt(ticket); 
      HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strEncryptedTicket); 
      Context.Response.Cookies.Add(cookie); 
      return true; 
     } 
     return false; 
    } 
} 

Sau tất cả điều này, bạn có thể gặp lỗi cấu hình cho phần bị khóa ở cấp độ gốc. Khóa theo mặc định (overrideModeDefault = "Deny") hoặc đặt rõ ràng bằng thẻ vị trí ... và nếu có, thì cách nhanh nhất để khắc phục đó là mở C: \ Windows \ System32 \ inetsrv \ config \ applicationHost.config và chỉnh sửa các khối sau:

<configSections> 
    <sectionGroup name="system.webServer"> 
    <sectionGroup name="security"> 
     <sectionGroup name="authentication"> 
     <section name="anonymousAuthentication" overrideModeDefault="Allow"> 
     <section name="windowsAuthentication" overrideModeDefault="Allow"> 
     </sectionGroup> 
    </sectionGroup> 
    </sectionGroup> 
</configSections> 

Cũng xem nhật ký trò chuyện: http://chat.stackoverflow.com/rooms/5/conversation/configuring-iis7-and-mixed-mode-authentication-in-asp-net

+0

này diễn ra chính xác như bạn mô tả, trong đó có lỗi Khóa vào cuối. Tôi lo lắng về các tác động của đề xuất "sửa chữa" của bạn. Tôi có mở ra một số lỗ hổng hoặc các vấn đề tiềm ẩn bằng cách ghi đè hành vi mặc định theo cách này không? Nó cảm thấy giống như một hack. Điều này quá cũ ... Tôi tự hỏi liệu có cách nào tốt hơn bây giờ không? – ctb

+0

Microsoft từ chối nó theo mặc định như một biện pháp bảo mật. Tất cả mọi thứ trong chỉnh sửa IIS metabase theo mặc định cảm thấy giống như một hack. Có thể có một số chức năng giao diện người dùng để cho phép bạn thay đổi điều đó nhưng tôi không thể bị phát hiện để tìm ra nó ở đâu. – jcolebrand

+0

Tùy thuộc vào người bạn muốn đến trước tiên – jcolebrand

Các vấn đề liên quan