1

私のアプリでは、ユーザーはWIF経由でログインしています。ユーザーの資格情報はに保存されSystem.Security.Principal.IIdentityます。CreateUser()今、私はメソッドをテストしたいと思います。私は何らかの方法でそのオブジェクトを偽造する必要があります。私は次のようなことをしようとしています:-そのオブジェクトを返すextractメソッド:

public IIdentity GetIdentity()
{
    return Thread.CurrentPrincipal.Identity;
}

次に、テストファイルで次のようにします。

    var identity = new GenericIdentity("mymail@mydomain.com");
    A.CallTo(() => _userRepository.GetIdentity()).Returns(identity);

しかし、それは機能しません。最善の方法でそれを行う方法はありますか?

4

2 に答える 2

1

WIFのClaimsPrincipalHttpModuleをご覧ください。STSを必要とせずにWindowsIDをIClaimsPrincipalに変換します。

また、非常に便利なツールであるSelfSTSもご覧ください。

于 2012-05-28T19:21:23.470 に答える
0

偽のWindowsIdentityクラスを作成します。

public class FakeWindowsIdentity : WindowsIdentity
{
    private readonly string _name;

    public FakeWindowsIdentity(string sUserPrincipalName) 
        : base(GetAnonymous())
    {
        _name = sUserPrincipalName;
    }

    public override string Name => _name;
}

私はFakeItEasyを使用しているIPrincipalので、次のような偽物を作成します。

IPrincipal fakeUser = A.Fake<IPrincipal>();
A.CallTo(() => fakeUser.Identity)
    .Returns(new FakeWindowsIdentity("domain\\username"));

スレッドに割り当てるには:

Thread.CurrentPrincipal = fakeUser;
于 2021-09-24T21:58:46.073 に答える