在 ASP.NET MVC 中模拟 User.Identity [英] Mocking User.Identity in ASP.NET MVC

查看:18
本文介绍了在 ASP.NET MVC 中模拟 User.Identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 ASP.NET MVC 2.0 网站创建单元测试.该站点使用 Windows 身份验证.

I need to create Unit Tests for an ASP.NET MVC 2.0 web site. The site uses Windows Authentication.

我一直在阅读有关为处理 HttpContext 的代码模拟 HTTP 上下文的必要性.我觉得我也开始掌握 DI 模式.(给类一个 IRepository 类型的属性,然后在实例化控制器时传入一个 Repository 对象.)

I've been reading up on the necessity to mock the HTTP context for code that deals with the HttpContext. I feel like I'm starting to get a handle on the DI pattern as well. (Give the class an attribute of type IRepository and then pass in a Repository object when you instantiate the controller.)

但是,我不明白的是,Mock 可通过 User.Identity 获得的 Windows Principal 对象的正确方法.这是 HttpContext 的一部分吗?

What I don't understand, however, is the proper way to Mock the Windows Principal object available through User.Identity. Is this part of the HttpContext?

是否有任何机构提供指向证明这一点的文章的链接(或推荐一本书)?

Does any body have a link to an article that demonstrates this (or a recommendation for a book)?

谢谢,

特雷·卡罗尔

推荐答案

我已经使用 IoC 将其抽象出来并取得了一些成功.我首先定义了一个类来表示当前登录的用户:

I've used IoC to abstract this away with some success. I first defined a class to represent the currently logged in user:

public class CurrentUser
{
    public CurrentUser(IIdentity identity)
    {
        IsAuthenticated = identity.IsAuthenticated;
        DisplayName = identity.Name;

        var formsIdentity = identity as FormsIdentity;

        if (formsIdentity != null)
        {
            UserID = int.Parse(formsIdentity.Ticket.UserData);
        }
    }

    public string DisplayName { get; private set; }
    public bool IsAuthenticated { get; private set; }
    public int UserID { get; private set; }
}

在构造函数中需要一个 IIdentity 来设置它的值.对于单元测试,您可以添加另一个构造函数以允许您绕过 IIdentity 依赖项.

It takes an IIdentity in the constructor to set its values. For unit tests, you could add another constructor to allow you bypass the IIdentity dependency.

然后我使用 Ninject(选择你最喜欢的 IoC 容器,没关系),并为 IIdentity 创建了一个绑定,如下所示:

And then I use Ninject (pick your favorite IoC container, doesn't matter), and created a binding for IIdentity as such:

Bind<IIdentity>().ToMethod(c => HttpContext.Current.User.Identity);

然后,在我的控制器内部,我在构造函数中声明了依赖项:

Then, inside of my controller I declare the dependency in the constructor:

CurrentUser _currentUser;

public HomeController(CurrentUser currentUser)
{
    _currentUser = currentUser;
}

IoC 容器看到 HomeController 采用 CurrentUser 对象,而 CurrentUser 构造函数采用 IIdentity.它将自动解决依赖关系,瞧!您的控制器可以知道当前登录的用户是谁.FormsAuthentication 对我来说似乎工作得很好.您或许可以将此示例改编为 Windows 身份验证.

The IoC container sees that HomeController takes a CurrentUser object, and the CurrentUser constructor takes an IIdentity. It will resolve the dependencies automatically, and voila! Your controller can know who the currently logged on user is. It seems to work pretty well for me with FormsAuthentication. You might be able to adapt this example to Windows Authentication.

这篇关于在 ASP.NET MVC 中模拟 User.Identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆