Mocking User.Identity in ASP.NET MVC

后端 未结 6 917
眼角桃花
眼角桃花 2020-12-13 06:23

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

I\'ve been reading up on the necessity to mock the HTTP context

6条回答
  •  一个人的身影
    2020-12-13 07:20

    I've changed dev environment global.asax and Web.Config for use FormsAuth for force a specific user. The username uses the same WindowsAuth format. See:

    public override void Init()
        {
            base.Init();
    
            this.PostAuthenticateRequest += 
                 new EventHandler(MvcApplication_PostAuthenticateRequest);
        }
    
        void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie("Domain\\login", true);
        }
    

    The Windows or Forms Auth shares the same login patterns.

    The application will work with both Windows authentication and Form authentication.

提交回复
热议问题