在ASP.NET MVC嘲讽User.Identity [英] Mocking User.Identity in ASP.NET MVC

查看:177
本文介绍了在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.

我一直在阅读了关于有必要嘲笑为code,与涉及的HttpContext的HTTP上下文。我觉得我开始变得对DI模式的手柄为好。 (给类类型IRepository的属性,然后传递一个信息库对象时,实例化控制器。)

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.)

我不明白,但是,是在嘲笑可通过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抽象送人取得了一些成功。我首先定义一个类来重新present当前登录的用户:

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 。它会自动解决的依赖性,和中提琴!您的控制器能知道谁是当前登录的用户。它似乎工作pretty很适合我用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 viola! 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天全站免登陆