{"_id":"553176c6e7d1fa0d003fc88e","project":"54471fc9e12a270800028adc","__v":0,"category":{"_id":"54485557c1b42e08005b82bf","__v":4,"version":"54471fc9e12a270800028adf","pages":["544855884544c30800241f54","54485598c1b42e08005b82c4","544855a6c1b42e08005b82c6","553176c6e7d1fa0d003fc88e"],"project":"54471fc9e12a270800028adc","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-23T01:09:43.240Z","from_sync":false,"order":7,"slug":"advanced-topics","title":"Advanced Topics"},"user":"54471f91beb6320800da6f75","version":{"_id":"54471fc9e12a270800028adf","__v":10,"project":"54471fc9e12a270800028adc","createdAt":"2014-10-22T03:08:57.750Z","releaseDate":"2014-10-22T03:08:57.750Z","categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-04-17T21:10:30.424Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Specifying your own ApplicationHost.config file for SpecsFor.Mvc to use is the secret to getting Windows Authentication to work.  I’m going to assume you’ve already got your web project configured to use Windows Authentication.  If not, that’s a can of worms for a different post (or perhaps Stack Overflow?)  This post will focus just on configuring the spec project.  First, let’s create our host file!\n\nAdd a new config file to your spec project called ApplicationHost.config.  Modify the properties for this file so that it’s copied to the output directory.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/suF9QsQVK1cG4sfnizxw_image.axd\",\n        \"image.axd\",\n        \"307\",\n        \"254\",\n        \"#20639b\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nNow drop in the standard XML for an IIS Express application config file.  If you don’t know what that is (I sure didn’t!), you can use  [this file from the sample project](https://github.com/MattHoneycutt/SpecsFor/blob/master/samples/SpecsForSamples/WindowsAuthSampleApp.Specs/applicationhost.config).\n\nThere are two changes you’ll need to make.  First, find the <sites> element, and change the name of the only site in the file to match the name of the project you’re writing specs against.  SpecsFor will overwrite this site definition at runtime.\n\nNext, jump to the end of the file, and add a location block (or change the path on the existing one if you’re using the config file from the sample), like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<location path=\\\"YOUR_PROJECT_NAME_HERE\\\">\\n  <system.webServer>\\n    <security>\\n      <authentication>\\n        <windowsAuthentication enabled=\\\"true\\\" />\\n        <anonymousAuthentication enabled=\\\"false\\\" />\\n      </authentication>\\n    </security>\\n  </system.webServer>\\n</location>\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\nAgain, be sure you set the path attribute to match the name of the project you are writing specs against!\n\nNow you’re ready to configure SpecsFor.Mvc!  Create a setup fixture in the root of your spec project, like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[SetUpFixture]\\npublic class SpecConfig\\n{\\n    private SpecsForIntegrationHost _host;\\n\\n    [SetUp]\\n    public void Setup()\\n    {\\n        var config = new SpecsForMvcConfig();\\n        config.UseIISExpress()\\n            .ApplicationHostConfigurationFile(:::at:::\\\"applicationhost.config\\\")\\n            .With(Project.Named(\\\"WindowsAuthSampleApp\\\"));\\n\\n        config.UseBrowser(BrowserDriver.InternetExplorer);\\n\\n        _host = new SpecsForIntegrationHost(config);            \\n        _host.Start();\\n    }\\n\\n    [TearDown]\\n    public void TearDown()\\n    {\\n        _host.Shutdown();\\n    }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow you can write a spec!  In my sample, I had the view output the name of the current user, which should match the current Windows Identity at runtime.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public class HomeControllerSpecs\\n{\\n    public class when_viewing_the_home_page : SpecsFor<MvcWebApp>\\n    {\\n        protected override void When()\\n        {\\n            SUT.NavigateTo<HomeController>(c => c.Index());\\n        }\\n\\n        [Test]\\n        public void then_it_has_the_name_of_the_logged_in_user()\\n        {\\n            SUT.FindDisplayFor<HomePageViewModel>()\\n                .DisplayFor(x => x.UserName).Text.ShouldEqual(WindowsIdentity.GetCurrent().Name);\\n        }\\n    }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThe full source for this sample is available in the [samples project on Github](https://github.com/MattHoneycutt/SpecsFor/tree/master/samples/SpecsForSamples/WindowsAuthSampleApp).","excerpt":"","slug":"using-specsformvc-with-windows-authentication","type":"basic","title":"Using SpecsFor.Mvc with Windows Authentication"}

Using SpecsFor.Mvc with Windows Authentication


Specifying your own ApplicationHost.config file for SpecsFor.Mvc to use is the secret to getting Windows Authentication to work. I’m going to assume you’ve already got your web project configured to use Windows Authentication. If not, that’s a can of worms for a different post (or perhaps Stack Overflow?) This post will focus just on configuring the spec project. First, let’s create our host file! Add a new config file to your spec project called ApplicationHost.config. Modify the properties for this file so that it’s copied to the output directory. [block:image] { "images": [ { "image": [ "https://files.readme.io/suF9QsQVK1cG4sfnizxw_image.axd", "image.axd", "307", "254", "#20639b", "" ] } ] } [/block] Now drop in the standard XML for an IIS Express application config file. If you don’t know what that is (I sure didn’t!), you can use [this file from the sample project](https://github.com/MattHoneycutt/SpecsFor/blob/master/samples/SpecsForSamples/WindowsAuthSampleApp.Specs/applicationhost.config). There are two changes you’ll need to make. First, find the <sites> element, and change the name of the only site in the file to match the name of the project you’re writing specs against. SpecsFor will overwrite this site definition at runtime. Next, jump to the end of the file, and add a location block (or change the path on the existing one if you’re using the config file from the sample), like so: [block:code] { "codes": [ { "code": "<location path=\"YOUR_PROJECT_NAME_HERE\">\n <system.webServer>\n <security>\n <authentication>\n <windowsAuthentication enabled=\"true\" />\n <anonymousAuthentication enabled=\"false\" />\n </authentication>\n </security>\n </system.webServer>\n</location>", "language": "xml" } ] } [/block] Again, be sure you set the path attribute to match the name of the project you are writing specs against! Now you’re ready to configure SpecsFor.Mvc! Create a setup fixture in the root of your spec project, like so: [block:code] { "codes": [ { "code": "[SetUpFixture]\npublic class SpecConfig\n{\n private SpecsForIntegrationHost _host;\n\n [SetUp]\n public void Setup()\n {\n var config = new SpecsForMvcConfig();\n config.UseIISExpress()\n .ApplicationHostConfigurationFile(@\"applicationhost.config\")\n .With(Project.Named(\"WindowsAuthSampleApp\"));\n\n config.UseBrowser(BrowserDriver.InternetExplorer);\n\n _host = new SpecsForIntegrationHost(config); \n _host.Start();\n }\n\n [TearDown]\n public void TearDown()\n {\n _host.Shutdown();\n }\n}", "language": "csharp" } ] } [/block] Now you can write a spec! In my sample, I had the view output the name of the current user, which should match the current Windows Identity at runtime. [block:code] { "codes": [ { "code": "public class HomeControllerSpecs\n{\n public class when_viewing_the_home_page : SpecsFor<MvcWebApp>\n {\n protected override void When()\n {\n SUT.NavigateTo<HomeController>(c => c.Index());\n }\n\n [Test]\n public void then_it_has_the_name_of_the_logged_in_user()\n {\n SUT.FindDisplayFor<HomePageViewModel>()\n .DisplayFor(x => x.UserName).Text.ShouldEqual(WindowsIdentity.GetCurrent().Name);\n }\n }\n}", "language": "csharp" } ] } [/block] The full source for this sample is available in the [samples project on Github](https://github.com/MattHoneycutt/SpecsFor/tree/master/samples/SpecsForSamples/WindowsAuthSampleApp).