且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Owin.TestServer测试需要身份验证的中间件

更新时间:2023-12-03 10:16:40

因此,这是我不为之兴奋的一种方法,但是它确实可以完成工作.我将等待接受,因为我认为应该有更好的方法.

So here is one way I suppose not thrilled with it but it does the job. I will wait to accept as I imagine there should be a better way.

public class TestFakeLoginMiddleware : OwinMiddleware
{
    public TestFakeLoginMiddleware(OwinMiddleware next) : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
        var identity = A.Fake<IIdentity>();
        A.CallTo(() => identity.Name).Returns("TEST@domain.local");
        var user = new ClaimsPrincipal(identity);
        context.Request.Context.Authentication.User = user;
        await Next.Invoke(context);
    }
}