解决模拟MOSS用户调用WebService打开个人站点进行操作
From:http://www.cnblogs.com/BruceLee521/
?
開始以為要構建
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
GenericPrincipal genericPrincipal =new GenericPrincipal(genericIdentity, roles);
然后把HttpContext.Current.User 置成上面構造的。但發現在打開個人站點時出錯。
然后修改個人站點C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\SPSPERS\default.aspx
加入
username:<%=HttpContext.Current.User.Identity.Name%>:
authen:<%=HttpContext.Current.User.Identity.AuthenticationType%>
authe:<%=HttpContext.Current.User.GetType().ToString()%>
identity:<%=HttpContext.Current.User.Identity.GetType().ToString()%>
運行發現頁面輸出
helloWORLD:sunsheng: authen:Forms authe:System.Web.Security.RolePrincipal identity:System.Web.Security.FormsIdentity
才知道構造用戶錯誤,應該構造
System.Web.Security.RolePrincipal
System.Web.Security.FormsIdentity
public class MOSSFBAImpersonate
??? {
??????? //code sample by Ric
??????? //================
??????? //? MOSSFBAImpersonate mossFBAImpersonate = new MOSSFBAImpersonate(ConfigurationManager.AppSettings.Get("RoleProviderName"));
??????? // FBAIP.Impersonate([roleProvidernameFromWebconfig],[impersonatingUserName]);
??????? //? //to do you work here....
??????? //? FBAIP.Revoke();
??????? //================
??????? #region public functions
??????? public MOSSFBAImpersonate(string roleProviderName)
??????? {
??????????? m_rolePrincipal = (IPrincipal)HttpContext.Current.User;
??????????? m_roleProvider = roleProviderName;
??????????? if (null == m_roleProvider)
??????????????? throw (new Exception("MOSSFBAImpersonate::roleProviderName is null!"));
??????? }
??????? public void Impersonate(string userName)
??????? {
??????????? HttpContext.Current.User = this.CreateHttpUser(m_roleProvider, userName);
??????? }
??????? public void Revoke()
??????? {
??????????? if (null != m_rolePrincipal)
??????????????? HttpContext.Current.User = (IPrincipal)m_rolePrincipal;
??????? }
??????? #endregion
??????? #region private region
??????? private object m_rolePrincipal = null;
??????? private string m_roleProvider = null;
??????? private RolePrincipal CreateHttpUser(string roleProviderName, string userName)
??????? {
??????????? GenericIdentity genericIdentity = new GenericIdentity(userName, "Forms");
??????????? return new RolePrincipal(roleProviderName, genericIdentity);
??????? }
??????? #endregion
??? }
來進行模擬用戶
得到個人站點的代碼可以如下:
public SPWeb GetPersonalWeb(string strAccount, string strPersonalSiteHost)
??????? {
??????????? SPWeb myWeb = null;
??????????? SPSite spPersonalSite = null;
??????????? MOSSFBAImpersonate mossFBAImpersonate = new MOSSFBAImpersonate(ConfigurationManager.AppSettings.Get("RoleProviderName"));
??????????? try
??????????? {
??????????????? mossFBAImpersonate.Impersonate(strAccount);
??????????????? spPersonalSite = new SPSite(strPersonalSiteHost);
??????????????? myWeb = spPersonalSite.RootWeb;
??????????????? mossFBAImpersonate.Revoke();
??????????? }
??????????? catch (System.Threading.ThreadAbortException thex)
??????????? {
??????????????? throw thex;
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
?????????????? //myWeb.Dispose(); //which will be disposed by outside caller.
??????????????? spPersonalSite.Dispose();
??????????? }
??????????? return myWeb;
??????? }
轉載于:https://www.cnblogs.com/Jeffer/archive/2009/11/26/1611632.html
總結
以上是生活随笔為你收集整理的解决模拟MOSS用户调用WebService打开个人站点进行操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 数据库添加,修改和删除
- 下一篇: docker安装mysql_Docker