2011-01-21 42 views
0

嗨 我想创建下的Web应用程序网站集被配置为基于索赔的身份验证和代码如下:添加网站集Web应用程序登录的FBA在SharePoint 2010

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 = {无法评估表达式,因为代码已经优化或本地框架位于调用堆栈之上。”)。任何在这方面的帮助真的很感激。
问候,
水稻

+0

我不认为这是问题的根源在这里,但你应该不处理`SPContext.Current.Site`属性返回的`SPSite`对象(即移除第一个'using`) - http://blogs.msdn.com/b/rogerla/archive/2008/02/12/sharepoint -2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_220 – 2011-01-21 22:55:28

+0

它从_layouts文件夹中的aspx页面调用。稻谷 – Paddy 2011-01-24 08:51:35

回答

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 
     } 
    } 
}); 
0

OwnerLogin参数应包含CustomMembership:前缀 - 通过普通用户名作为该参数的值。


顺便说一句,你得到的Web应用程序对象的方法过于复杂 - 用这样的:

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