将网站集添加到在SharePoint 2010中以FBA身份登录的Web应用程序

时间:2011-01-21 19:36:31

标签: sharepoint-2010

您好 我正在尝试在Web应用程序下创建网站集,该网站应用程序配置为基于声明的身份验证,代码如下:

SPSecurity.RunWithElevatedPrivileges(delegate {
  using (SPSite site = SPContext.Current.Site)
  {
    using (SPWeb web = site.RootWeb)
    {
      site.AllowUnsafeUpdates = true;
      web.AllowUnsafeUpdates = true;
      try
      {
        SPWebApplication web_App = web.Site.WebApplication;
        web_App.Sites.Add(SiteUrl, SiteTitle, Description, Convert.ToUInt32(Constants.LOCALE_ID_ENGLISH), SiteTemplate, OwnerLogin, "testuser", OwnerEmail);
      }
      catch (Exception ex)
      { 
        string s = ex.Message + " " + ex.StackTrace;
        throw;
      }
      finally
      {
        web.AllowUnsafeUpdates = false;
        site.AllowUnsafeUpdates = false;
      }
    }
  }
});

这里我将“OwnerLogin”作为“CustomMembership:UserName”传递。但是web_App.Sites.Add抛出了一个奇怪的错误,例如“ex = {无法评估表达式,因为代码已经过优化,或者本机框架位于调用堆栈之上。”。在这方面的任何帮助都非常感谢 问候,
稻谷

2 个答案:

答案 0 :(得分:1)

您的提升代码错误,您需要创建全新的SPSite和SPWeb引用。我正常地用“c”作为前缀,告诉我它处于不同的上下文中。

SPSecurity.RunWithElevatedPrivileges(delegate() {
    using (SPSite csite = new SPSite(SPContext.Current.Site.ID)) {
        using (SPWeb cweb = csite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
            //do stuff
        }
    }
});

答案 1 :(得分:0)

OwnerLogin参数应包含 CustomMembership:前缀 - 将普通 UserName 作为此参数的值传递。< / p>


顺便说一句,获取Web应用程序对象的方法不必要地复杂化 - 使用类似这样的东西:

SPWebApplication webApplication = SPWebApplication.Lookup(new System.Uri("Web-Application-URL"));