Wiki内検索
最近更新したページ
最新コメント
メニューバー
タグ

SharePointのコードアクセスセキュリティについて

■[WSS3.0(MOSS2007)] SharePointのコードアクセスセキュリティについて

SharePoint(WSS3.0、MOSS2007)でのプログラム開発の際、SharePointのカレントユーザの権限に応じて、特定のコードにアクセスできなくなる現象があります。(SharePointのコードアクセスセキュリティによるものです。)

そのコードにアクセスするためには、高い権限を持つユーザでログインし直すか、プログラム内部においてユーザ偽装をする必要があります。

例えば、閲覧権限しか持たないユーザでログインしたときには、ユーザ偽装を使用しなければプログラム上からリストアイテムの登録をすることはできませんが、システムアカウントのユーザに偽装することによって、プログラム上からリストアイテムの登録ができるようになります。

ユーザ偽装を行うサンプルコードを例に示します。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Security.Permissions;
using System.Security.Principal;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Upgrade;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

protected override void OnLoad(EventArgs e)
{
    // 偽装操作を行う前の Windows ユーザー
    WindowsImpersonationContext wic = null;
    try
    {
        // ドメイン名、ユーザ名、パスワードを設定する
        string sDomain = @"domain_name";
        string sUserAlias = @"user_name";
        string sPassword = @"password";

        // アカウント偽装処理
        IntPtr tokenHandle = new IntPtr(0); // The Windows NT user token.

        const int LOGON32_PROVIDER_DEFAULT = 0;
        const int LOGON32_LOGON_NETWORK = 3;

        tokenHandle = IntPtr.Zero;

        // 上で設定したユーザー資格情報を用いてアカウントを偽装をする
        bool returnValue = LogonUser(sUserAlias, sDomain, sPassword,
            LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

        if (false == returnValue)
        {
            int ret = Marshal.GetLastWin32Error();
            throw new Exception("LogonUser failed with error code: " + ret);
        }

        //The WindowsIdentity class makes a new copy of the token.
        //It also handles calling CloseHandle for the copy.
        WindowsIdentity id = new WindowsIdentity(tokenHandle);
        CloseHandle(tokenHandle);

        // アカウント偽装メソッド。
        // このメソッドを実行することで、ユーザ偽装が行われる。
        wic = id.Impersonate();

        //     :
        // この中でユーザ偽装後の処理を実装する
        //     :
    }
    catch
    {
        throw;
    }
    finally
    {
        // アカウント偽装終了処理。
        // ユーザ偽装後の処理で例外が発生した場合に備えて、
        // 必ずfinally句でアカウント偽装終了処理を実装する。
        if (wic != null)
        {
            wic.Undo();
        }
    }
}

// ユーザ偽装を使用するためには、
// 下のDLLが必要。
// DLLImportを使用する。

#region DllImport
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
    int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
#endregion


■注意点■

aspxファイルでの「コードブロックを有効にする」設定を行っている場合、偽装ユーザ処理を組み込んだaspxファイルをSharePointサーバ上にアップロードされてしまう恐れがあります。
セキュリティ上、致命的な事態となりますので、できるだけ、aspxファイルでの「コードブロックを有効にする」設定を行わないことを推奨します。

※aspxファイルのコードブロックを無効にする設定にしてある場合は、サーバーサイドスクリプトを埋め込んだaspxファイルをアップロードされても機能しません。


■回避方法■

(方法:1) 拡張子「aspx」のファイルをアップロード不可にする。
[設定方法]
  WSS3.0の「サーバーの全体管理」サイトの、
  サーバーの全体管理 -> サーバー構成の管理 -> ブロックするファイルの種類
  のページから設定できます。

(方法:2) aspxファイルでのコードブロックを有効にしない。
[設定方法]
  SharePointが実行されているIISサーバのホームディレクトリ直下のWeb.Configで、
  <SafeMode>タグの内容を以下のように記述する。
<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
    <PageParserPaths>
</PageParserPaths>
</SafeMode>
(方法:3) 偽装ユーザを実行できないようにコードアクセスセキュリティの設定を見直す。
[設定方法]
  SharePointが実行されているIISサーバのホームディレクトリ直下のWeb.Configで、
  <trust>タグの内容を以下のように記述する。
<trust level="WSS_Minimal" originUrl=""/>
2007年05月08日(火) 00:34:43 Modified by sharepoint




スマートフォン版で見る