2014-11-07 10 views
5

Làm cách nào để thiết lập ứng dụng biểu mẫu web có nhận dạng và nợ để từ chối tất cả các trang ngoại trừ thông tin đăng nhập?Từ chối tất cả các trang mà không cần đăng nhập vào các Biểu mẫu Web Asp.net có Khung Nhận dạng và Owin

cấu hình trong web.config này không làm việc cho tôi:

<system.web> 
    <authorization> 
     <deny users="*"/> 
    </authorization> 
    <authentication mode="None"/> 

Thông báo lỗi: Yêu cầu mô-đun lọc được cấu hình để từ chối yêu cầu nơi chuỗi truy vấn quá dài.

OWIN lớp khởi động:

public void ConfigureAuth(IAppBuilder app) 
     { 
      // Configure the db context, user manager and signin manager to use a single instance per request 
      app.CreatePerOwinContext(ApplicationDbContext.Create); 
      app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
      app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 

      // Enable the application to use a cookie to store information for the signed in user 
      // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
      // Configure the sign in cookie 
      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
       LoginPath = new PathString("/Account/Login"), 
       Provider = new CookieAuthenticationProvider 
       { 
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, Usuario>(
         validateInterval: TimeSpan.FromMinutes(0), 
         regenerateIdentity: (manager, user) => manager.GenerateUserIdentityAsync(user)) 
       } 
      }); 

cấu trúc dự án enter image description here

Edit:

Mở web.config bên trong thư mục tài khoản có cấu hình này.

<configuration> 

    <location path="Manage.aspx"> 
    <system.web> 
     <authorization> 
     <allow users="?"/> 
     </authorization> 
    </system.web> 
    </location> 

</configuration> 

Làm việc này cho trang Manage.aspx.

Tôi không muốn làm điều này cho mọi trang. Tôi muốn đưa vào web.config toàn cầu của trang web.

Trả lời

-1

if (User.Identity.IsAuthenticated) { nghỉ trên trang } khác { chuyển đến khác}

+0

Tôi nghĩ rằng không cần phải thực hiện xác thực mã cứng này. – Copo

0

Bạn chỉ có thể cấu hình nó trong web.config của bạn như thế này:

EDIT: Đã thêm cấu hình cho chuỗi yêu cầu dài thêm

Nếu yêu cầu của bạn trở nên quá dài, bạn có thể thêm này trong web.config của bạn để khắc phục những vấn đề:

<system.webServer> 
    <security> 
    <requestFiltering> 
     <requestLimits maxQueryString="nnn"/> 
    </requestFiltering> 
    </security> 
</system.webServer> 

Tôi hy vọng điều này sửa chữa nó ngay bây giờ.

+0

Cấu hình này không hoạt động. Thông báo lỗi: Mô-đun lọc yêu cầu được định cấu hình để từ chối yêu cầu trong đó chuỗi truy vấn quá dài. – Copo

+0

Tôi nghĩ điều này không cần thiết. Url không được dài. – Copo

1

Tôi đã thử nghiệm rất nhiều với Web.config và luôn luôn có lỗi như đã mô tả ở đây. Sau đó, tôi đưa nó lên và chỉ cần thêm một bộ lọc để Global.asax

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    string cTheFile = HttpContext.Current.Request.Path; 
    if (!cTheFile.EndsWith("Login")) 
    { 
    if (HttpContext.Current.User == null || 
     HttpContext.Current.User.Identity == null || 
     !HttpContext.Current.User.Identity.IsAuthenticated) 
    { 
     Response.Redirect("~/Account/Login", true); 
     Response.End(); 
     return; 
    } 
    } 
} 

này đã làm việc tốt cho tôi, mặc dù tôi không chắc chắn, nếu nó là một giải pháp tối ưu.

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