2012-04-23 111 views
3

希望這很簡單。我有一個相當簡單的ASP.NET(框架版本2)應用程序,它使用自定義表格進行用戶驗證。無論如何,我有兩個頁面,登錄和註冊。你可以猜測目的是什麼。用戶應該能夠通過點擊註冊鏈接來請求註冊 - 這是一個帶有提交按鈕的表單,用於執行一些數據庫調用來查看用戶是否存在等等。登錄頁面使用驗證Cookie進行驗證。我使用窗體身份驗證 - 這是我的web.config:ASP.NET - 如果來自注冊頁面,請停止重定向到登錄頁面

<authentication mode="Forms"> 
     <forms loginUrl="logon.aspx" name="adAuthCookie" timeout="30" path="/" defaultUrl="~/logon.aspx"> 
     </forms> 
    </authentication> 

每次我做一個HTTP調用到註冊頁面(在http://localhost/registration.aspx鍵入IE瀏覽器 - 它重定向到登錄頁面

global.asax.cs文件有這個內容 - 這是一個身份驗證檢查,如果請求的頁面是註冊頁面,我想禁用這個檢查 - 因爲用戶不需要通過身份驗證來訪問此頁面。這個?

void Application_AuthenticateRequest(object sender, EventArgs e) 
{ 
    string cookieName = FormsAuthentication.FormsCookieName; 
    HttpCookie authCookie = Context.Request.Cookies[cookieName]; 

    if (null == authCookie) 
    { 
     //There is no authentication cookie. 
     return; // right here it will return null then redirect to login.aspx 
    } 
    FormsAuthenticationTicket authTicket = null; 
    try 
    { 
     authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
    } 
    catch (Exception ex) 
    { 
     //Write the exception to the Event Log. 
     return; 
    } 
    if (null == authTicket) 
    { 
     //Cookie failed to decrypt. 
     return; 
    } 
    //When the ticket was created, the UserData property was assigned a 
    //pipe-delimited string of group names. 
    string[] groups = authTicket.UserData.Split(new char[] { '|' }); 
    //Create an Identity. 
    GenericIdentity id = new GenericIdentity(authTicket.Name, "LdapAuthentication"); 
    //This principal flows throughout the request. 
    GenericPrincipal principal = new GenericPrincipal(id, groups); 
    Context.User = principal; 
} 
+0

您能通過字符串比較檢查,如果請求的URL包含字符串「註冊」,而不是返回 – rt2800 2012-04-23 14:31:17

+0

檢查這個http://stackoverflow.com/questions/3628445/允許訪問未認證用戶對特定頁面使用asp-net-forms-authe – Aristos 2012-04-23 14:36:01

回答

2

您可以配置你的web.config訪問設置。這裏是什麼,我會做一個例子:

<location path="register.aspx"> //path here is path to your register.aspx 
    <system.web> 
     <authorization> 
      <allow users="*"/> // this will allow access to everyone to register.aspx 
     </authorization> 
    </system.web> 
</location> 
+0

非常感謝,這個工程很棒。 – Rob 2012-04-23 14:40:32

+0

我的榮幸!另外,作爲一個旁觀者,@Sanjay提供了一個不錯的鏈接它描述了它的許多用途,包括修改文件夾級別的訪問權限,這意味着您可以將所有與註冊相關的頁面分組到一個註冊目錄中,並提供對該目錄的訪問,就像您在每頁上一樣考慮到這一點,我通常會放置所有的頁面(default.aspx除外) nto Pages目錄,並保護整個文件夾,而不會限制對主頁的訪問(default.aspx)。 – Jeremy 2012-04-23 14:56:18

2

它可以幫你 與web.config中

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

更多信息MSDN鏈接http://support.microsoft.com/kb/316871

相關問題