3流プログラマのメモ書き

元開発職→社内SE→派遣で営業支援の三流プログラマのIT技術メモ書き。 このメモが忘れっぽい自分とググってきた技術者の役に立ってくれれば幸いです。(jehupc.exblog.jpから移転中)

(.Net).Netで作成したアセンブリを共有フォルダで実行すると落ちる

(.Net Framework は 2.0です)

同じセグメント内の共有フォルダ内の .Net アプリケーションは問題なく動きました。

問題はルータを超えて共有フォルダにアクセスするときです。

(例えば192.168.0.xxxのPCが192.168.1.xxxの共有フォルダ上の .Net アプリを動かす時です)

その場合、アプリケーションを起動すると、まず下記のような警告ダイアログがでます。(社内で使う用のツールなので、当然デジタル署名は付けてません。)

そして、実行を選ぶと「問題が発生したため アプリ名 を終了します。」と例外が発生し、起動できません。。

この実行できずにエラーで落ちる件は、.Net のコードアクセスセキュリティというものが関係しているらしいです。

調べてみると、解決策が”熱狂するシステム開発!”ブログ:【C#】ネットワークドライブ上にあるアセンブリを実行するに載ってました。

どうやらコードアクセスセキュリティを変更すればいいようです。

GUIの「Microsoft .NET Framework 2.0 構成」ツールはSDK入れないと無いようなので、フレームワーク自体についている caspol.exe を使うとコマンドラインで設定できるようですね。

とりあえず、

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>caspol.exe -machine -chggroup LocalIntranet_Zone FullTrust

で確認を聞いてくるので yes を押下するといいようです。

で、実行したけど同じエラーが出てしまいました。。

よく考えたら、上記のコマンド上ではゾーンが LocalIntranet になっているのに、共有フォルダをエクスプローラ上でみると、セキュリティゾーンがインターネットになってます。(XPだとステータスバー右下)

ということで、インターネットオプションダイアログ開き、共有フォルダのパス(file://192.168.1.xxx)をローカルイントラネットゾーンに追加してみました。

これで実行したら動きました。

さて、この.Netのコードアクセスセキュリティ構成をリセットするには下記コマンド入れるといいらしいです。

caspol.exe -all -reset

コードアクセスセキュリティも概要は知っとく必要と思って、MSDNみたけど、どうもわかりやすい情報ありません。

比較的下記サイトがわかりやすかったです。

.NET Framework におけるセキュリティ

さらに詳しい説明は.Net1.1ですが、下記が参考になります。(ただ読むのに時間かかりますが..)

@IT:インサイド .NET Framework [改訂版]第6回 コード・アクセス・セキュリティ(その1)

ちなみに、現在のコードグループ表示は下記MSDNに載ってます。

MSDN:方法 : Caspol.exe を使用してコード グループを表示する

今回、上記の設定後のコードグループを確認してみるとこうなりました。

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>caspol -machine -listgroups

Microsoft (R) .NET Framework CasPol 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.

Security is ON

Execution checking is ON

Policy change prompt is ON

Level = Machine

Code Groups:

1. All code: Nothing

1.1. Zone - MyComputer: FullTrust

1.1.1. StrongName - 0024document48document94document00602document0240000525341310004document100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB

03766C834C99921EB23BE79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E821C0A5EFE8F1645C4C0C93C1AB99285D622CAA6

52C1DFAD63D745D6F2DE5F17E5EAF0FC4963D261C8A12436518206DC093344D5AD293: FullTrust

1.1.2. StrongName - documentdocumentdocument004documentdocument0000: FullTrust

1.2. Zone - Intranet: FullTrust

1.2.1. All code: Same site Web

1.2.2. All code: Same directory FileIO - 'Read, PathDiscovery'

1.3. Zone - Internet: Internet

1.3.1. All code: Same site Web

1.4. Zone - Untrusted: Nothing

1.5. Zone - Trusted: Internet

1.5.1. All code: Same site Web

Success

この結果をみても、コードグループが1.1とか1.2とかわけわからんので、コード グループの名前と説明を一覧表示してみます。

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>caspol -machine -listdescription

Microsoft (R) .NET Framework CasPol 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.

Security is ON

Execution checking is ON

Policy change prompt is ON

Level = Machine

Code Groups:

1. All_Code: Code group grants no permissions and forms the root of the code group tree.

1.1. My_Computer_Zone: Code group grants full trust to all code originating on the local computer

1.1.1. Microsoft_Strong_Name: Code group grants full trust to code signed with the Microsoft strong name.

1.1.2. ECMA_Strong_Name: Code group grants full trust to code signed with the ECMA strong name.

1.2. LocalIntranet_Zone: Code group grants the intranet permission set to code from the intranet zone. This permission set grants intran

et code the right to use isolated storage, full UI access, some capability to do reflection, and limited access to environment variables.

1.2.1. Intranet_Same_Site_Access: All intranet code gets the right to connect back to the site of its origin.

1.2.2. Intranet_Same_Directory_Access: All intranet code gets the right to read from its install directory.

1.3. Internet_Zone: Code group grants code from the Internet zone the Internet permission set. This permission set grants Internet code

the right to use isolated storage and limited UI access.

1.3.1. Internet_Same_Site_Access: All Internet code gets the right to connect back to the site of its origin.

1.4. Restricted_Zone: Code coming from a restricted zone does not receive any permissions.

1.5. Trusted_Zone: Code from a trusted zone is granted the Internet permission set. This permission set grants the right to use isolated

storage and limited UI access.

1.5.1. Trusted_Same_Site_Access: All Trusted Code gets the right to connect back to the site of its origin.

Success

これで、どの番号がどのゾーンかわかりますね。

ただ、配布する先のクライアントPCにいちいちコマンド打つの面倒なので、下記のようにバッチファイルを走らすと楽です。

echo y|%sytemroot%\Microsoft.NET\Framework\v2.0.50727\caspol.exe -machine -chggroup LocalIntranet_Zone FullTrust

インターネットゾーンに対して FullTrust アクセス許可セットを指定すると共有フォルダのイントラネットへの追加をしなくてもいいんでしょうが、さすがにセキュリティのリスクが高すぎるというか危険ですね。

また、これは試してないので何とも言えませんが、ActiveDirectory環境なら、caspol の第一引数のポリシーレベルは enterprise にしてもいいんかもしれません。