2010-08-19 78 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