C#之模擬AD用戶請求Sharepoint網站


最近在用Sharepoint時,碰到一個奇怪的問題:針對協作站點和文檔中心(Blog、Wiki、Meeting等不會出現這種情況),當系統賬戶憑據失效時,其他用戶均無法登陸或者操作顯示異常。也就是說,必須系統賬戶訪問協作站點或者文檔中心后,在一段時間內,其他用戶才能登陸或者訪問協作站點和文檔中心。

站點結構如圖:

image

相關異常日志如下:

 

w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Logging Correlation Data          xmnv    Medium      Name=Request (GET:http://10.10.10.112:80/aviage/SitePages/Home.aspx)    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           72k8    High        Failed to determine the setup path of the list schema for feature {00BFEA71-E717-4E80-AA17-D0C71B360101}, list template 101.    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Logging Correlation Data          xmnv    Medium      Site=/    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Monitoring                        b4ly    High        Leaving Monitored Scope (PostResolveRequestCacheHandler). Execution Time=15.0540781021052    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           72k8    High        Failed to determine the setup path of the list schema for feature {00BFEA71-C796-4402-9F2F-0EB9A6E71B18}, list template 119.    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           xxph    High        Failed to get the Fields schema with 0x8107058A    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           8sl1    High        DelegateControl: Exception thrown while adding control 'ASP._controltemplates_publishingconsole_ascx': Operation is not valid due to the current state of the object.    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           72k8    High        Failed to determine the setup path of the list schema for feature {00BFEA71-E717-4E80-AA17-D0C71B360101}, list template 101.    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Web Parts                         7935    Information    http://10.10.10.112/aviage/SitePages/Home.aspx - An unexpected error has been encountered in this Web Part.  Type: Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Error: An error has occurred.    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           72k8    High        Failed to determine the setup path of the list schema for feature {00BFEA71-C796-4402-9F2F-0EB9A6E71B18}, list template 119.    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             General                           xxph    High        Failed to get the Fields schema with 0x8107058A    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Runtime                           tkau    Unexpected    System.InvalidOperationException: Operation is not valid due to the current state of the object.    at Microsoft.SharePoint.SPUserToken.GetClaimsUserLoginName()     at Microsoft.SharePoint.SPSite.CopyUserToken(SPUserToken userToken)     at Microsoft.SharePoint.SPSite.SPSiteConstructor(SPFarm farm, Guid applicationId, Guid contentDatabaseId, Guid siteId, Guid siteSubscriptionId, SPUrlZone zone, Uri requestUri, String serverRelativeUrl, Boolean hostHeaderIsSiteName, SPUserToken userToken)     at Microsoft.SharePoint.SPSite..ctor(Guid id, SPFarm farm, SPUrlZone zone, SPUserToken userToken)     at Microsoft.SharePoint.SPSite..ctor(Guid id, SPFarm farm, SPUrlZone zone)     at Microsoft.SharePoint.SPFeatureManager.EnsureSiteAndWeb(SPUrlZone zone, Guid databaseid, Guid siteid, Guid webid, SPSite&...    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Runtime                           tkau    Unexpected    ... site, SPWeb& web)     at Microsoft.SharePoint.SPFeatureManager.<>c__DisplayClass19.<GetFeatureRootAndListSchemaPaths>b__18()     at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param)     at Microsoft.SharePoint.SPFeatureManager.GetFeatureRootAndListSchemaPaths(Byte[]& userToken, Guid& tranLockerId, Int32 nZone, Guid databaseid, Guid siteid, Guid webid, Guid featid, Int32 ltid, String& sPathToFeatureRoot, String& sPathToSchemaXml)     at Microsoft.SharePoint.Library.SPRequestInternalClass.GetListItemDataWithCallback2(IListItemSqlClient pSqlClient, String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback,...    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Runtime                           tkau    Unexpected    ... ISPDataCallback pPagingPrevCallback, ISPDataCallback pFilterLinkCallback, ISPDataCallback pSchemaCallback, ISPDataCallback pRowCountCallback, Boolean& pbMaximalView)     at Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback2(IListItemSqlClient pSqlClient, String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pPagingPrevCallback, ISPDataCallback pFilterLinkCallback, ISPDataCallback pSchemaCallback, ISPDataCallback pRowCountCallback, Boolean& pbMaximalView)     at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()     at Microsoft.SharePoint.SPListItemCollection.get_Count()     at Microsoft.SharePoint.SPList.GetItemById(Strin...    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Runtime                           tkau    Unexpected    ...g strId, Int32 id, String strRootFolder, Boolean cacheRowsetAndId, String strViewFields, Boolean bDatesInUtc)     at Microsoft.SharePoint.SPContext.get_Item()     at Microsoft.SharePoint.SPContext.get_Fields()     at Microsoft.SharePoint.WebControls.FieldMetadata.get_Field()     at Microsoft.SharePoint.WebControls.FormField.CreateChildControls()     at System.Web.UI.Control.EnsureChildControls()     at Microsoft.SharePoint.WebPartPages.WikiEditPage.OnLoad(EventArgs e)     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)    7b7ff55b-fbdb-442c-96df-10cd59c5691a
w3wp.exe (0x15B8)    0x1048    SharePoint Foundation             Monitoring                        b4ly    Medium      Leaving Monitored Scope (Request (GET:http://10.10.10.112:80/aviage/SitePages/Home.aspx)). Execution Time=36.9820046961276    7b7ff55b-fbdb-442c-96df-10cd59c5691a

關鍵異常如下:

at Microsoft.SharePoint.SPUserToken.GetClaimsUserLoginName()     at Microsoft.SharePoint.SPSite.CopyUserToken(SPUserToken userToken)     at Microsoft.SharePoint.SPSite.SPSiteConstructor(SPFarm farm, Guid applicationId, Guid contentDatabaseId, Guid siteId, Guid siteSubscriptionId, SPUrlZone zone, Uri requestUri, String serverRelativeUrl, Boolean hostHeaderIsSiteName, SPUserToken userToken)

初步診斷發現,是同時集成Windows驗證和Form驗證導致的,因為建設同樣架構的站點,不集成form驗證則不會出現該問題。根據日志搜索,折騰半天,發現老外遇到過,但是無解決方案,不知道是否為SharePoint的BUG,因為Form驗證仔細檢查過,並不存在問題,那么只能自己想辦法了。於是誕生了使用C#模擬系統賬戶登陸來請求相關頁面了。關鍵代碼如下:

private static void RequestUrl(string url, int i)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        //下面三行代碼,啟用任意一行都是可以的。
        //request.UseDefaultCredentials = true;
        //request.Credentials = CredentialCache.DefaultCredentials;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6.4; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        //request.Credentials = CredentialCache.DefaultNetworkCredentials;
        var cre = new NetworkCredential("sharepoint", "Sharepointadmin", "域名");
        //request.Credentials = cre;
        request.Credentials = cre.GetCredential(new Uri(url), String.Empty);
        request.Accept = "*/*";
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                var content = sr.ReadToEnd().ToString();
                var path = Path.Combine(Application.StartupPath, "WebPages");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = Path.Combine(path, string.Format("{0}.txt", i));
                File.WriteAllText(path, content, Encoding.UTF8);
            }
        }
    }
    catch (WebException wex)
    {
        NLog.LogManager.GetCurrentClassLogger().ErrorException("請求異常", wex);
    }
}

代碼是比較簡單的,但也折騰了一會,因為關於模擬AD驗證的關鍵字搜到的都是做AD驗證而不是模擬AD請求驗證,根據MSDN的說明,下面三行代碼應該都是可用的:

//request.UseDefaultCredentials = true;
//request.Credentials = CredentialCache.DefaultCredentials;
//request.Credentials = CredentialCache.DefaultNetworkCredentials;

因為都是默認使用當前Windows用戶進行請求,可是放服務器上運行發現是拒絕訪問的。幾經折騰,才發現只有上面的方式才能實現請求。

模擬請求的實現了,那么只要每隔一段時間模擬系統賬戶請求相關頁面,那么問題就解決了,而且頁面顯示的速度也會快很多。

最后,如果有同學解決了上面那個SharePoint問題,請通知我,畢竟我這種方式並不是很“光彩”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM