天天看點

SharePoint2010 技巧系列:巧用HttpModule

需求背景:

本公司建立了兩個站點,一個是對内站點,隻允許公司的内部員工通路,另一個是外部站點,這裡的外部是指公司的合作商或者外包商可以通路的站點。對這兩個站點的要求是内部的站點非本公司員工不可以通路, 由于内部的站點允許所有員工通路,是以我們添加了“ALL authenticated Users” 這就意味着,凡是Active Directory的使用者都能通路,是以這裡面使用者就包括了合作商和外包商使用者(公司統一使用AD認證,合作商和外包商有對應的Active Directory 賬戶)。

解決方案: 由于内部站點不允許外部員工通路(外部員工被統一存放在AD Externals 組内),我們的目标就是組織Externals 組通路我們的内部站點,查過微軟的官方文檔,也Google很多資料,得出結論,SharePoint隻能在Web Application層次上對使用者群組進行限制,不能在站點集或者站點上進行拒絕通路。是以我們隻能改變原來的架構,把不同的站點分别放在不同的Web Application上。

那還有沒有别的方案?反正Google不到,後來突然想到從IIS 層次

需求背景:

本公司建立了兩個站點,一個是對内站點,隻允許公司的内部員工通路,另一個是外部站點,這裡的外部是指公司的合作商或者外包商可以通路的站點。對這兩個站點的要求是内部的站點非本公司員工不可以通路, 由于内部的站點允許所有員工通路,是以我們添加了“ALL authenticated Users” 這就意味着,凡是Active Directory的使用者都能通路,是以這裡面使用者就包括了合作商和外包商使用者(公司統一使用AD認證,合作商和外包商有對應的Active Directory 賬戶)。

解決方案: 由于内部站點不允許外部員工通路(外部員工被統一存放在AD Externals 組内),我們的目标就是組織Externals 組通路我們的内部站點,查過微軟的官方文檔,也Google很多資料,得出結論,SharePoint隻能在Web Application層次上對使用者群組進行限制,不能在站點集或者站點上進行拒絕通路。是以我們隻能改變原來的架構,把不同的站點分别放在不同的Web Application上。

那還有沒有别的方案?反正Google不到,後來突然想到從IIS 層次上去想解決方案,于是就想到了HttpModule,經過測試好像還真的可行。

首先,打開Visual Studio 2010, 建立一個Class Library。

其次,建立一個class,實作IHttpModule

HttpModule 代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Microsoft.SharePoint;
using System.ServiceModel;
using System.Net;
using PermissionChecker.MWADService;
namespace PermissionChecker
{
public class MWLinkChecker : IHttpModule
    {
public void Dispose() { }

public void Init(HttpApplication context)        
        {
            context.PostAuthenticateRequest += new EventHandler(context_PostAuthenticateRequest);
        }


void context_PostAuthenticateRequest(object sender, EventArgs e)
        {
//check whether current site is internal site
            if (HttpContext.Current.Request.Url.AbsolutePath.Contains("InternalSupport"))
            {

                SPUser user = null;

try
                {
#region 
//HttpRequest request = ((HttpApplication)sender).Request;
                    ////Host Domain
                    //String requestUrlDomain = "http://" + request.Url.Host;

////Previous Host Domain
                    //String previousRequestUrlDomain = String.Empty;
//if (request.UrlReferrer != null)
//{
//    previousRequestUrlDomain = "http://" + request.UrlReferrer.Host;
//}

////If coming from within same host, no redirection required
                    //if (!requestUrlDomain.Equals(previousRequestUrlDomain))
//{

#endregion
//Getting the HttpContext
                        HttpContext context = ((HttpApplication)sender).Context;

//Creating SPSite object
                        SPSite spSite;
//Creating SPWeb object
                        SPWeb spWeb;
//Checking for the current SPContext
                        if (SPContext.Current != null)
                        {
//Getting the SPSite
                            spSite = SPContext.Current.Site;
//Getting the SPWeb
                            spWeb = spSite.RootWeb;
//Get the SPUser
                            user = spWeb.CurrentUser;

//call web service to check whether current user is in EXTERNALS Group
                            MWADService.MWADInfoSoapClient client;

                            BasicHttpBinding binding = new BasicHttpBinding();
                            binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
                            client = new MWADInfoSoapClient(binding, new EndpointAddress("web service URL"));
                            client.Endpoint.Binding = binding;
                            client.ClientCredentials.Windows.ClientCredential = new NetworkCredential(@"Domain\Account", "Password");

                            MWADService.ArrayOfString groups = client.GetGroupsForUser(user.LoginName);

if (groups.Contains("EXTERNALS"))
                            {
                                HttpContext.Current.Response.Redirect("access denied page");
                            }
                        }
// }
// }
                }
catch
                {

                }
            }
        }
    }
}
      

第三步,在對應的站點目錄下web.config中加入以下内容。

<httpModules>
      <add name="MWLINKCheckerSetting" type="PermissionChecker.MWLinkChecker,PermissionChecker, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ec7365e9b36581d1" />
      <add name="FederatedAuthentication" type="Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <add name="SessionAuthentication" type="Microsoft.SharePoint.IdentityModel.SPSessionAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <add name="SPWindowsClaimsAuthentication" type="Microsoft.SharePoint.IdentityModel.SPWindowsClaimsAuthenticationHttpModule, Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
     
    </httpModules>
      

第四步:部署DLL 到GAC。

第五步,測試效果。