Creating MSI Packages using VSO Build Services – A simple approach

With VS 2012 (and above), those versions have removed the capability to create deployment projects. We use 3rd party tools to build setup/MSI packages at the moment. This works very well for us in our on-prem environments. With the integration of cloud services, we took the approach of consolidating most of the build process and output to our cloud instance. This also means, having the capabilities of hosted (cloud) build controllers to make use of other 3rd party tools (in this case build MSI’s) however, we cannot install additional apps on hosted cloud build controllers. The other approach was to use hybrid solution where we use build services on the cloud however the actual build controller resides on our on-prem infrastructure. This was an approach that I’m willing to take but I’d like to take it further and really push all the mechanisms to the cloud. Further researching, VS 2012 (and above) still has the capability to develop setup projects. The only downside is that: It’s a separate extension in Visual Studio (which is really not a big concern) and that MSBUILD doesn’t support building deployment project files (.VDPROJ).

Luckily, devenv.exe (when used correctly) allows you to build solutions with deployment project files. More importantly, you can run it on a command line and with the right switches, you can build deployment projects files which outputs MSI packages.

First and foremost, use these extension package to install deployment project templates for Visual Studio (2012) and above:

Microsoft Visual Studio 2013 Installer Projects
https://visualstudiogallery.msdn.microsoft.com/9abe329c-9bba-44a1-be59-0fbf6151054d

Further context, I’m not going to elaborate on the steps to create deployment projects using the templates in Visual Studio, however, here’s a very good article that I’m going to reference to get you started:

Visual Studio Create Setup project to deploy web application in IIS
http://www.aspdotnet-suresh.com/2012/04/visual-studio-create-setup-project-to.html

Note that given some of our MSIs are used for deploying web applications (Yes, let’s have the discussion later why we use MSI’s for web deployments J), the article above is suitable for this context.The end result though is generating MSI’s regardless whether it’s a Web or Windows setup project.

Steps:

Create a powershell script that calls devenv process and invoke parameters for the location of your solution file, project file and configuration (release, debug, etc…)

The exact syntax would be:

Param(
[string]$SolutionPath,
[string]$ProjectPath,
[string]$ConifugrationMode
)

Start-Process -FilePath "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv" -ArgumentList "$SolutionPath /build $ConifugrationMode /project $ProjectPath /projectconfig $ConifugrationMode"

Save the powershell script in source control

In our case, we use VSO GIT. The script file doesn’t need to go on any folder structure as you will see later.

Create a build definition in VSO Build

Start of by creating a build definition in VSO. Go to “Builds” in your team project, click on the “+”.

Select Visual Studio. (Note that probably this UI may change given that features and changes do occur more frequently in VSO).

MSI1

“Deleting” and “Adding” the correct build steps

Given that we’re using devenv process to build the solution and generate the MSI packages, we don’t need the initial steps to build the code with the deployment project. All we need for this example are 2 steps

Run a powershell script:

MSI2

Publish build artifacts:

MSI3

Powershell Build Step
The key for this step is to ensure that you provide the correct set of variables for the location for your solution and project file.

Script filename: Basically click on the ellipses and browse through your repository to select the powershell script that runs devenv.exe

Arguments: Make sure that you use the correct configuration variables. Simply copy and paste this line:

.\<yourpowershellscript>.ps1 -SolutionPath “$(Build.Repository.LocalPath)\<locationofthesln>\XXX.sln” -ProjectPath “$(Build.Repository.LocalPath)\<locationofthevdproj>\XXX.VDPROJ” -ConifugrationMode $(BuildConfiguration)

The variables that I used here are:
$(Build.Repository.LocalPath) = this is where the build controller will hold the temporary build files

$(BuildConfiguration) = configuration setting that’s under the “Variables” section for your build:

MSI4

NOTE: Here’s a complete list of build variables for VSO:
https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/variables.md

Publish Build Artifacts Step

Contents: select the location where your MSI is located. I used the following syntax since my MSI packages are under setup\release\**
**\\Release\**

Artifact Name: This is just the name of your drop location.

Complete Build definition

Here’s an image of what it should like including a successful build:

MSI5

MSI6

MSI7

Using OpenID to authenticate in MVC via Azure AD (Manual Steps)

Title says it all, we have some MVC apps using Azure AD via WSFed and want to convert using OpenID auth. While WSFED works well, we wanted to take a simple approach of using OpenID through Azure AD. These are the steps to either convert from WSFED or add OpenID in existing MVC Apps for Authentication.

I assume that you already have an application registered in Azure Active Directory for your website to use for authenticating AD users. If not, the first step is to create an Application in Azure Active Directory for your website to use to authenticate AD users. To do this:

    1. Sign in to the Azure Management Portal (http://azure.microsoft.com).
    2. Click on the Active Directory icon on the left menu, and then click on the desired directory.
    3. On the top menu, click Applications. If no apps have been added to your directory, this page will only show the Add an App link. Click on the link, or alternatively you can click on the Add button on the command bar.
    4. On the What do you want to do page, click on the link to Add an application my organization is developing.
  • On the Tell us about your application page, you must specify a name for your application as well as indicate the type of application you are registering with Azure AD. You can choose from a web application and/or web API (default) or native client application which represents an application that is installed on a device such as a phone or computer. For this guide, make sure to select Web Application and/or Web API
  1. Once finished, click the arrow icon on the bottom-right corner of the page.
  2. On the App properties page, provide the Sign-on URL (URL for your web application) and App ID URI (Unique URI for your application – Usually it’s a combination or your AD domain/application. Example: http://www.domain.com/mywebsite.somedomain.com) for your web application then click the checkbox in the bottom-right hand corner of the page.
  3. Your application has been added, and you will be taken to the Quick Start page for your application.
  4. Click on the “Configure” Tab. Generate a Key for your client access and write down the following information:
      1. CLIENT ID:
      2. KEY (You generate a Key by clicking on the Save Button on the configure tab)
      3. APP ID URI

     

  5. Federation Metadata Document (You can get this information by click on “VIEW ENDPOINTS” at the bottom section of the Configure tab)

 Capture

Enable SSL on your Dev Machines

With OpenID, you need to have your MVC app enabled with SSL. In your development environment, you can set this by going to the properties of the MVC app, select “Web” on the left navigation and type “https” on the project URL box:

SSL

Add OpenID and OWIN nuget packages to your MVC Application:

  • Microsoft.IdentityModel.Protocol.Extensions
  • System.IdentityModel.Tokens.Jwt
  • Microsoft.Owin.Security.OpenIdConnect
  • Microsoft.Owin.Security.Cookies
  • Microsoft.Owin.Host.SystemWeb
  • Active Directory Authentication Library

Create a class Startup.Auth.cs in the App_Start folder

Replace the code from below: Be sure to take the whole class definition!

Namespace references:


using Microsoft.IdentityModel.Clients.ActiveDirectory;

using Microsoft.Owin.Security;

using Microsoft.Owin.Security.Cookies;

using Microsoft.Owin.Security.OpenIdConnect;

using Owin;


public partial class Startup

   {

       //

       // The Client ID is used by the application to uniquely identify itself to Azure AD.

       // The App Key is a credential used to authenticate the application to Azure AD. Azure AD supports password and certificate credentials.

       // The Metadata Address is used by the application to retrieve the signing keys used by Azure AD.

       // The AAD Instance is the instance of Azure, for example public Azure or Azure China.

       // The Authority is the sign-in URL of the tenant.

       // The Post Logout Redirect Uri is the URL where the user will be redirected after they sign out.

       //

       private static string clientId = ConfigurationManager.AppSettings[&quot;ida:ClientId&quot;];

       private static string appKey = ConfigurationManager.AppSettings[&quot;ida:AppKey&quot;];

       private static string aadInstance = ConfigurationManager.AppSettings[&quot;ida:AADInstance&quot;];

       private static string tenant = ConfigurationManager.AppSettings[&quot;ida:Tenant&quot;];

       private static string postLogoutRedirectUri = ConfigurationManager.AppSettings[&quot;ida:PostLogoutRedirectUri&quot;];

       public static readonly string Authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

       // This is the resource ID of the AAD Graph API. We'll need this to request a token to call the Graph API.

       string graphResourceId = ConfigurationManager.AppSettings[&quot;ida:GraphUrl&quot;];

       public void ConfigureAuth(IAppBuilder app)

       {

           app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

           app.UseCookieAuthentication(new CookieAuthenticationOptions());

           app.UseOpenIdConnectAuthentication(

               new OpenIdConnectAuthenticationOptions

               {

                   ClientId = clientId,

                   Authority = Authority,

                   PostLogoutRedirectUri = postLogoutRedirectUri,

                  Notifications = new OpenIdConnectAuthenticationNotifications()

                   {

                       //

                       // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.

                       //

                       AuthorizationCodeReceived = (context) =&gt;

                       {

                           var code = context.Code;

                           ClientCredential credential = new ClientCredential(clientId, appKey);

                           string userObjectID = context.AuthenticationTicket.Identity.FindFirst(

                                   &quot;http://schemas.microsoft.com/identity/claims/objectidentifier&quot;).Value;

                           AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID));

                           AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(

                              code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);

                           AuthenticationHelper.token = result.AccessToken;

                           return Task.FromResult(0);

                       }

                   }

               });

       }

   }

Create Utility classes

In the project, create a new folder called Utils, create a class AuthenticationHelper.cs. Replace the code from below. Be sure to take the whole class definition!

References


using Microsoft.Azure.ActiveDirectory.GraphClient;


internal class AuthenticationHelper

   {

       public static string token;

       /// &lt;summary&gt;

       ///     Async task to acquire token for Application.

       /// &lt;/summary&gt;

       /// &lt;returns&gt;Async Token for application.&lt;/returns&gt;

       public static async Task&lt;string&gt; AcquireTokenAsync()

       {

           if (token == null || token.IsEmpty())

           {

               throw new Exception(&quot;Authorization Required.&quot;);

           }

           return token;

       }

       /// &lt;summary&gt;

       ///     Get Active Directory Client for Application.

       /// &lt;/summary&gt;

       /// &lt;returns&gt;ActiveDirectoryClient for Application.&lt;/returns&gt;

       public static ActiveDirectoryClient GetActiveDirectoryClient()

       {

           Uri baseServiceUri = new Uri(Constants.ResourceUrl);

           ActiveDirectoryClient activeDirectoryClient =

               new ActiveDirectoryClient(new Uri(baseServiceUri, Constants.TenantId),

                   async () =&gt; await AcquireTokenAsync());

           return activeDirectoryClient;

       }

   }

In the Utils folder, create a class Constants.cs. Replace the code from below. Be sure to take the whole class definition!


internal class Constants

   {

       public static string ResourceUrl = ConfigurationManager.AppSettings[&quot;ida:GraphUrl&quot;];

       public static string ClientId = ConfigurationManager.AppSettings[&quot;ida:ClientId&quot;];

       public static string AppKey = ConfigurationManager.AppSettings[&quot;ida:AppKey&quot;];

       public static string TenantId = ConfigurationManager.AppSettings[&quot;ida:TenantId&quot;];

       public static string AuthString = ConfigurationManager.AppSettings[&quot;ida:Auth&quot;] +

                                         ConfigurationManager.AppSettings[&quot;ida:Tenant&quot;];

       public static string ClientSecret = ConfigurationManager.AppSettings[&quot;ida:ClientSecret&quot;];

   }

In the Utils folder, create three new classes called NaiveSessionCache.cs. Replace the code from below. Be sure to take the whole class definition!

References:


using Microsoft.IdentityModel.Clients.ActiveDirectory;


public class NaiveSessionCache : TokenCache

   {

       private static readonly object FileLock = new object();

       private readonly string CacheId = string.Empty;

       private string UserObjectId = string.Empty;

       public NaiveSessionCache(string userId)

       {

           UserObjectId = userId;

           CacheId = UserObjectId + &quot;_TokenCache&quot;;

           AfterAccess = AfterAccessNotification;

           BeforeAccess = BeforeAccessNotification;

           Load();

       }

       public void Load()

       {

           lock (FileLock)

           {

               if (HttpContext.Current != null)

               {

                   Deserialize((byte[])HttpContext.Current.Session[CacheId]);

               }

           }

       }

        public void Persist()

       {

           lock (FileLock)

           {

               // reflect changes in the persistent store

               HttpContext.Current.Session[CacheId] = Serialize();

               // once the write operation took place, restore the HasStateChanged bit to false

               HasStateChanged = false;

           }

       }

       // Empties the persistent store.

       public override void Clear()

       {

           base.Clear();

           HttpContext.Current.Session.Remove(CacheId);

       }

       public override void DeleteItem(TokenCacheItem item)

       {

           base.DeleteItem(item);

           Persist();

       }

       // Triggered right before ADAL needs to access the cache.

       // Reload the cache from the persistent store in case it changed since the last access.

       private void BeforeAccessNotification(TokenCacheNotificationArgs args)

       {

           Load();

       }

       // Triggered right after ADAL accessed the cache.

       private void AfterAccessNotification(TokenCacheNotificationArgs args)

       {

           // if the access operation resulted in a cache update

           if (HasStateChanged)

           {

               Persist();

           }

       }

   }

Add OWIN Startup class 

Right-click on the project, select Add, select “OWIN Startup class”, and name the class “Startup”. If “OWIN Startup Class” doesn’t appear in the menu, instead select “Class”, and in the search box enter “OWIN”. “OWIN Startup class” will appear as a selection; select it, and name the class Startup.cs .

In Startup.cs , replace the code from below. Again, note the definition changes from public class Startup to public partial class Startup .


using System;

using System.Threading.Tasks;

using Microsoft.Owin;

using Owin;

[assembly: OwinStartup(typeof(MVCProject.Startup))]

namespace MVCProject

{

   public partial class Startup

   {

       public void Configuration(IAppBuilder app)

       {

           ConfigureAuth(app);

       }

   }

}

Create UserProfile model

In the Models folder add a new class called UserProfile.cs . Copy the implementation of UserProfile from below:


public class UserProfile

   {

       public string DisplayName { get; set; }

       public string GivenName { get; set; }

       public string Surname { get; set; }

   }

Create new UserProfileController

Add a new empty MVC5 controller UserProfileController to the project. Copy the implementation from below. Remember to include the [Authorize] attribute on the class definition.

References:


using System.Net.Http;

using System.Net.Http.Headers;

using System.Security.Claims;

using System.Threading.Tasks;

using System.Web;

using System.Web.Mvc;

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using Microsoft.Owin.Security.OpenIdConnect;

using Newtonsoft.Json;


[Authorize]

   public class UserProfileController : Controller

   {

       private const string TenantIdClaimType = &quot;http://schemas.microsoft.com/identity/claims/tenantid&quot;;

       private static readonly string clientId = ConfigurationManager.AppSettings[&quot;ida:ClientId&quot;];

       private static readonly string appKey = ConfigurationManager.AppSettings[&quot;ida:AppKey&quot;];

       private readonly string graphResourceId = ConfigurationManager.AppSettings[&quot;ida:GraphUrl&quot;];

       private readonly string graphUserUrl = &quot;https://graph.windows.net/{0}/me?api-version=&quot; +

                                              ConfigurationManager.AppSettings[&quot;ida:GraphApiVersion&quot;];

       //

       // GET: /UserProfile/

       public async Task&lt;ActionResult&gt; Index()

       {

           //

           // Retrieve the user's name, tenantID, and access token since they are parameters used to query the Graph API.

           //

           UserProfile profile;

           string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;

            AuthenticationResult result = null;

           try

           {

               // Get the access token from the cache

               string userObjectID =

                   ClaimsPrincipal.Current.FindFirst(&quot;http://schemas.microsoft.com/identity/claims/objectidentifier&quot;)

                       .Value;

               AuthenticationContext authContext = new AuthenticationContext(Startup.Authority,

                   new NaiveSessionCache(userObjectID));

               ClientCredential credential = new ClientCredential(clientId, appKey);

               result = authContext.AcquireTokenSilent(graphResourceId, credential,

                   new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

               // Call the Graph API manually and retrieve the user's profile.

               string requestUrl = String.Format(

                   CultureInfo.InvariantCulture,

                   graphUserUrl,

                   HttpUtility.UrlEncode(tenantId));

               HttpClient client = new HttpClient();

               HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);

               request.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, result.AccessToken);

               HttpResponseMessage response = await client.SendAsync(request);

               // Return the user's profile in the view.

               if (response.IsSuccessStatusCode)

               {

                   string responseString = await response.Content.ReadAsStringAsync();

                   profile = JsonConvert.DeserializeObject&lt;UserProfile&gt;(responseString);

               }

               else

               {

                   // If the call failed, then drop the current access token and show the user an error indicating they might need to sign-in again.

                   authContext.TokenCache.Clear();

                   profile = new UserProfile();

                   profile.DisplayName = &quot; &quot;;

                   profile.GivenName = &quot; &quot;;

                   profile.Surname = &quot; &quot;;

                   ViewBag.ErrorMessage = &quot;UnexpectedError&quot;;

               }

           }

           catch (Exception e)

           {

               if (Request.QueryString[&quot;reauth&quot;] == &quot;True&quot;)

              {

                   //

                   // Send an OpenID Connect sign-in request to get a new set of tokens.

                   // If the user still has a valid session with Azure AD, they will not be prompted for their credentials.

                   // The OpenID Connect middleware will return to this controller after the sign-in response has been handled.

                   //

                   HttpContext.GetOwinContext()

                       .Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);

               }

               //

               // The user needs to re-authorize. Show them a message to that effect.

               //

               profile = new UserProfile();

              profile.DisplayName = &quot; &quot;;

               profile.GivenName = &quot; &quot;;

               profile.Surname = &quot; &quot;;

               ViewBag.ErrorMessage = &quot;AuthorizationRequired&quot;;

           }

           return View(profile);

       }

   }

Create new AccountController

Add a new empty MVC5 controller AccountController to the project. Copy the implementation from below.

References:


using System.Security.Claims;

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using Microsoft.Owin.Security;

using Microsoft.Owin.Security.Cookies;

using Microsoft.Owin.Security.OpenIdConnect;

using QualityEngineeringSite.Utils;


public class AccountController : Controller

   {

       public void SignIn()

       {

           // Send an OpenID Connect sign-in request.

           if (!Request.IsAuthenticated)

           {

               HttpContext.GetOwinContext()

                   .Authentication.Challenge(new AuthenticationProperties { RedirectUri = &quot;/&quot; },

                       OpenIdConnectAuthenticationDefaults.AuthenticationType);

           }

       }

       public void SignOut()

       {

           // Remove all cache entries for this user and send an OpenID Connect sign-out request.

           string userObjectID =

               ClaimsPrincipal.Current.FindFirst(&quot;http://schemas.microsoft.com/identity/claims/objectidentifier&quot;).Value;

           AuthenticationContext authContext = new AuthenticationContext(Startup.Authority,

               new NaiveSessionCache(userObjectID));

           authContext.TokenCache.Clear();

           AuthenticationHelper.token = null;

           HttpContext.GetOwinContext().Authentication.SignOut(

               OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);

       }

   }

Create a new partial view _LoginPartial.cshtml 

In the Views –> Shared folder, create a new partial view _LoginPartial.cshtml. Replace the contents of the file from below


@using System

@{

   var user = &quot;Null User&quot;;

   if (!String.IsNullOrEmpty(User.Identity.Name))

   {

       user = User.Identity.Name;

   }

}

@if (Request.IsAuthenticated)

{

   &lt;text&gt;

       &lt;ul class=&quot;nav navbar-nav navbar-right&quot;&gt;

           &lt;li&gt;

               @Html.ActionLink(user, &quot;Index&quot;, &quot;UserProfile&quot;, routeValues: null, htmlAttributes: null)

           &lt;/li&gt;

           &lt;li&gt;

               @Html.ActionLink(&quot;Sign out&quot;, &quot;SignOut&quot;, &quot;Account&quot;)

           &lt;/li&gt;

       &lt;/ul&gt;

   &lt;/text&gt;

}

else

{

   &lt;ul class=&quot;nav navbar-nav navbar-right&quot;&gt;

       &lt;li&gt;@Html.ActionLink(&quot;Sign in&quot;, &quot;Index&quot;, &quot;UserProfile&quot;, routeValues: null, htmlAttributes: new { id = &quot;loginLink&quot; })&lt;/li&gt;

   &lt;/ul&gt;

}

Modify existing _Layout.cshtml

In the Views –> Shared folder, add a single line, @Html.Partial(“_LoginPartial”) , that lights up the previously added _LoginPartial view. See screenshot below

Authenticate Users

If you want the user to be required to sign-in before they can see any page of the app, then in the HomeController, decorate the HomeController class with the [Authorize] attribute. If you leave this out, the user will be able to see the home page of the app without having to sign-in first, and can click the sign-in link on that page to get signed in.

For more information around the AuthorizeAttribute, refer to:

AuthorizeAttribute Class

https://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx

Web.Config Settings

In web.config , in <appSettings> , create keys for ida:ClientId , ida:AppKey , ida:AADInstance , ida:Tenant and ida:PostLogoutRedirectUri and set the values accordingly. For the public Azure AD, the value of ida:AADInstance is https://login.windows.net/{0} . See sample below:


&lt;!-- Values for OpenID and Graph API --&gt;

&lt;!-- ClientId is the application ID from your own Azure AD tenant --&gt;

   &lt;add key=&quot;ida:ClientId&quot; value=&quot;XXXXXXX&quot; /&gt;

   &lt;add key=&quot;ida:AppKey&quot; value=&quot;XXXXX&quot; /&gt;

   &lt;add key=&quot;ida:AADInstance&quot; value=&quot;https://login.windows.net/{0}&quot; /&gt;

&lt;!-- Tenant is the Tenant ID from your own Azure AD tenant. This is in a form of GUID.This is the value from your Federation Metadata Document URL' --&gt;

   &lt;add key=&quot;ida:Tenant&quot; value=&quot;XXXXXXXXX&quot; /&gt;

&lt;!-- Tenant is the Tenant ID from your own Azure AD tenant. This is in a form of GUID.This is the value from your Federation Metadata Document URL' --&gt;

   &lt;add key=&quot;ida:TenantId&quot; value=&quot;XXXXXXXX&quot; /&gt;

&lt;!-- PostLogoutRedirectUri is your application endpoint --&gt;

   &lt;add key=&quot;ida:PostLogoutRedirectUri&quot; value=&quot;http://xxxx.azurewebsites.net/&quot; /&gt;

   &lt;add key=&quot;aspnet:UseTaskFriendlySynchronizationContext&quot; value=&quot;true&quot; /&gt;

In web.config add this line in the <system.web> section: <sessionState timeout=”525600″ /> . This increases the ASP.Net session state timeout to its maximum value so that access tokens and refresh tokens cache in session state aren’t cleared after the default timeout of 20 minutes.