2012-05-08 100 views
0

ASP.NET:我創建了一個包含登錄驗證的網站。在用戶可以訪問其中的任何頁面之前,他需要先登錄。如何提示用戶先登錄才能查看網站的任何內容?提示用戶進行登錄驗證

+1

你檢查http://support.microsoft.com/kb/301240? – rt2800

回答

1

嘗試做這在你的web.config

如果我們想拒絕匿名用戶訪問,配置以下列方式授權部分,

<configuration> 
    <system.web> 
     <authentication mode="Forms"/> 
     <authorization> 
      <deny users="?" /> 
     </authorization> 
    </system.web> 
</configuration> 

欲瞭解更多信息看here

2

檢查ASP.NET's Membership, Roles, and Profile series是一個很好的起點。它涵蓋了ASP.NET的所有安全部分和你所需要的東西,在通過LoginUrl作爲登錄頁面訪問頁面之前登錄。開始這樣做How To: Use Forms Authentication with SQL Server in ASP.NET 2.0

在web.config中進行一些設置,然後在代碼背後處理這些事情。

<forms name=".ASPXAUTH" loginUrl="login.aspx" 
     defaultUrl="default.aspx" protection="All" timeout="30" path="/" 
     requireSSL="false" slidingExpiration="true" 
     cookieless="UseDeviceProfile" domain="" 
     enableCrossAppRedirects="false"> 
    <credentials passwordFormat="SHA1" /> 
</forms> 

在Web.config文件的元素下添加以下元素。這允許所有經過身份驗證的用戶訪問您的網站。

<authorization> 
    <deny users="?" /> 
    <allow users="*" /> 
</authorization>` 

代碼背後

if (Membership.ValidateUser(username, password)) 
{ 
    // User has supplied valid credentials 

    // In the following method call, the second Boolean parameter 
    // determines whether a persistent authentication cookie 
    // is created. 

    FormsAuthentication.RedirectFromLoginPage(username, rememberMeIsChecked); 
} 

參考:

Starting ASP.NET Forms Authentication
ASP.NET Authentication Explained: Forms Authentication in ASP.NET 2.0

+0

感謝@FilBurt的編輯..我在編輯器時遇到了格式化問題。 –

1

這是很複雜的問題,以支付張貼代碼示例下面就o我會推薦以下步驟。

使用asp.net MVC3,學會成員提供工作,如何定製它,如果適合你的需要,用戶角色提供給用戶分配到您將用於保護網站的特定區域的特定羣體。

創建角色,並將其分配給用戶之後,你可以用裝飾[授權]屬性或爲固定用戶選擇這樣的

[Authorize(Roles = "Admin, Super User")] 

使用你的web.config中配置的System.Web節到安全網頁指出應用程序中使用了哪些成員資格和角色提供者

這是簡短的信息,但我希望你現在有簡明的心理圖景。