在 ASP.NET MVC 中模拟 User.Identity

Mocking User.Identity in ASP.NET MVC(在 ASP.NET MVC 中模拟 User.Identity)
本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to MOQ an Indexed property(如何最小起订量索引属性)
Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
How do I mock a class without an interface?(如何模拟没有接口的类?)
Mocking Static methods using Rhino.Mocks(使用 Rhino.Mocks 模拟静态方法)