2010-08-19 91 views
0

使用標準的ASP.NET登錄控制,我試圖讓它拒絕我登錄,如果用戶誰試圖登錄成員的取消組。ASP.NET登錄控制 - 拒絕登錄,如果在一個角色

<asp:Login ID="Login1" runat="server" OnLoggedIn="Login1_LoggedIn" DestinationPageUrl="~/Default.aspx" FailureText="Whoops - something went wrong with your login. <br />Please try again. <br />If you've forgotton you password please click link below and we'll send you a new one" > 
    <LayoutTemplate> 
     <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
      <tr> 

和後面的代碼(僞代碼..不工作)

protected void Login1_LoggedIn(object sender, EventArgs e) 
    { 
     var profile = WebProfile.GetProfile(UserManager.GetCurrentUserName()); 

     if User.IsInRole("cancelled") { 
      FormsAuthentication.SignOut(); 
      // write out a message saying you are not allowed to login 
     } 
    } 

回答

0

其實,你不必做這件事的代碼。您可以放入web.config的授權部分來阻止訪問,而不是禁止登錄。讓用戶登錄但沒有權限是完全合法的。

<deny roles="canceled"> 

如果你仔細想想,這是有道理的。我們必須通過設置

所以有人登錄的其他用戶級別將被鎖定,具有通過把一個web.config鎖定限制管理員文件夾中的網站。使用我的建議,你特別鎖定用戶在配置文件中的「取消」角色,這通常更適合編譯需要編譯的代碼。

+0

不錯的一個...乾杯 – 2010-08-19 07:05:53