删除用户警报时,用户代码未处理System.UnauthorizedAccessException?

时间:2011-04-04 21:25:47

标签: sharepoint sharepoint-2007 wss

通过编程删除用户提醒时,我收到System.UnauthorizedAccessException was unhandled by user code。它在我的质量保证农场工作得很好。但不在DEV农场工作。

我将应用程序池帐户添加到服务器场管理组和数据库用户组dbowner。仍然得到同样的错误。

 protected void ChkBx41_CheckedChanged(object sender, EventArgs e)
  {
    SPUser user = SPContext.Current.Web.CurrentUser;       
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     using(SPSite site=new SPSite(url.Text)){
      using (SPWeb eweb = site.OpenWeb())
      {
            SPUser juser = null;
            eweb.AllowUnsafeUpdates = true;
            try
            {
              juser = eweb.AssociatedMemberGroup.Users[user.LoginName];
            }
            catch (Exception)
            {
            }
            if (ChkBx41.Checked)
            {

                if (juser == null)
                {
                  eweb.AssociatedMemberGroup.AddUser(eweb.EnsureUser(user.LoginName));
                  SPUtility.SendEmail(eweb, true, true, user.Email, "Welcome to " + eweb.Title, "Hello " + "<br><br>" + "Welcome to the " + eweb.Title + " ");
                  createalert(SPAlertFrequency.Daily, eweb, eweb.EnsureUser(user.LoginName),true);
             }
             RBList4.SelectedValue = "Daily";
             RBList4.Enabled = true;
           }
           else
           {
             if (juser != null)
             {
                eweb.AssociatedMemberGroup.RemoveUser(juser);
                removealert(eweb, juser);
                RBList4.SelectedValue = null;
                RBList4.Enabled = false;
             }

          }
      eweb.AllowUnsafeUpdates = false;
    }
  }
});


}

    public void removealert(SPWeb rweb, SPUser ruser)
      {
        bool oldCatchAccessDeniedException = rweb.Site.CatchAccessDeniedException;
        try
        {
          SPUser cuser = rweb.EnsureUser(ruser.LoginName);
          List<Guid> altid = new List<Guid>();
          foreach (SPAlert alt in cuser.Alerts)
          {
            try
            {
              if (alt.AlertType == SPAlertType.List)
              {
                altid.Add(alt.ID);
              }
            }
            catch (Exception) { }
          }
          rweb.Site.CatchAccessDeniedException = false;
          foreach (Guid delid in altid)
          {
            cuser.Alerts.Delete(delid);
          } 
        }
        catch (UnauthorizedAccessException)
        {
        }
        finally
        {
          rweb.Site.CatchAccessDeniedException = oldCatchAccessDeniedException;
        }
       }      

1 个答案:

答案 0 :(得分:2)

从网络部分运行,尝试提升?

public void removealert(SPWeb rweb, SPUser ruser) {
    SPSecurity.RunWithElevatedPrivileges(delegate() {
        using(SPSite csite = new SPSite(rweb.Site.Id)) {
            using(SPWeb cweb = csite.OpenWeb(rweb.Id)) {
                SPUser cuser = cweb.EnsureUser(ruser.LoginName);    

                List<Guid> altid = new List<Guid>();
                foreach (SPAlert alt in cuser.Alerts) {
                    try {
                        if (alt.AlertType == SPAlertType.List) {
                            altid.Add(alt.ID);
                        }
                    } catch (Exception) {
                    }
                }
                foreach (Guid delid in altid) {
                    cuser.Alerts.Delete(delid);
                }
            }
        }
    });
}

我也很想知道SPWeb rweb的来源。我希望不要来自SPContext,因为你不应该处理它。

我建议您将SPWebs放在一个使用块中,在相同的方法块中以便于阅读。