2014-11-07 5 views
5

Wie richte ich eine Web Forms Anwendung mit Identität ein und lege dazu alle Seiten mit Ausnahme der Anmeldung ab?Verweigern Sie alle Seiten ohne Anmeldung auf Asp.net Web Forms mit Identity Framework und Owin

Diese Konfiguration in web.config nicht für mich arbeiten:

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

Fehlermeldung: Das Anforderungsfilterungsmodul ist konfiguriert, um eine Anfrage zu verweigern, wenn die Abfrage-Zeichenfolge zu lang ist.

OWIN Startklasse:

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)) 
       } 
      }); 

Projektstruktur enter image description here

Edit:

Auf web.config innerhalb Konto Ordner gibt es diese Konfiguration.

<configuration> 

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

</configuration> 

Dies funktioniert für Manage.aspx Seite.

Ich möchte das nicht für jede Seite tun. Ich möchte die globale web.config der Website einfügen.

Antwort

-1

if (User.Identity.IsAuthenticated) { Aufenthalt auf Seite } sonst { Übertragung auf eine andere }

+0

Ich denke, es ist nicht notwendig, diese harte Code-Validierung zu machen. – Copo

0

Sie es so in Ihrer web.config nur konfigurieren können:

BEARBEITEN: Konfiguration für extra lange Anfragezeichenfolge hinzugefügt

Wenn Ihre Anfrage zu lang wird, können Sie dies in Ihrer web.config hinzufügen, um das Problem zu überwinden:

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

Ich hoffe, das es jetzt fixiert.

+0

Diese Konfiguration funktioniert nicht. Errorachricht: Das Anforderungsfiltermodul ist so konfiguriert, dass eine Anforderung abgelehnt wird, wenn die Abfragezeichenfolge zu lang ist. – Copo

+0

Ich denke, das sollte nicht notwendig sein. Die URL sollte nicht lang sein. – Copo

1

experimentierte ich viel mit Web.config und immer Fehler hatte, wie bereits beschrieben. Dann gab ich es auf und haben soeben einen Filter auf 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; 
    } 
    } 
} 

Das funktionierte gut für mich, obwohl ich nicht sicher bin, ob es eine optimale Lösung ist.

Verwandte Themen