- 追加された行はこの色です。
- 削除された行はこの色です。
Open棟梁 wiki
「[[Open棟梁 wiki>https://opentouryo.osscons.jp]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。
-[[戻る>IdentityImpersonation]]
*目次 [#x05788f7]
#contents
*概要 [#s3e7414f]
区間偽装のサンプルです。
Open棟梁の[[IdentityImpersonation]].[[ImpersonateValidUser>https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Security/IdentityImpersonation.cs#L128]]+[[UndoImpersonation>https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Security/IdentityImpersonation.cs#L220]]メソッドを使用しています。
try
{
// コードの特定部分を実行するときのみ、任意のユーザを偽装する。
// 偽装して
ii = new IdentityImpersonation();
ret = ii.ImpersonateValidUser("x", "", "x");
// 画像ファイルを開く
// 存在チェック
this.lblElse.Text
+= string.Format("、偽装後(任意のユーザ「{0}」を偽装):", WindowsIdentity.GetCurrent().Name)
+ ResourceLoader.LoadAsString(@"c:\test.txt", Encoding.GetEncoding(CustomEncode.UTF_8));
// ファイルを開く
Console.WriteLine(
string.Format("ユーザ「{0}」を偽装):", WindowsIdentity.GetCurrent().Name)
+ ResourceLoader.LoadAsString(@"c:\test.txt", Encoding.GetEncoding(CustomEncode.UTF_8)));
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
finally
{
// 偽装解除
ret = ii.UndoImpersonation();
}