Creating new Web Application in code

No comments

A couple of days ago I wrote some code for creating a web application, configuring Claims Authentication and adding some alternate urls.
I will share here the code with you:

SPWebApplicationBuilder builder = new SPWebApplicationBuilder(SPFarm.Local); // This class is used for creating the web application.
SPWebApplication webApp;

builder.Port = 80; // Custom port number
builder.AllowAnonymousAccess = true; // If you want to allow anonymous access to the web application
builder.HostHeader = "CustomHostHeader";
builder.IdentityType = IdentityType.SpecificUser; // The process identity type of the application pool
builder.ApplicationPoolId = "ApplicationPool Name"; // The application pool name where you want the web app to be created
builder.RootDirectory = new System.IO.DirectoryInfo("RootDirectoryPath"); // The path to the root directory: C:\Inetpub\wwwroot\...
builder.CreateNewDatabase = true;
builder.DatabaseName = "WSS_Content_CustomWebApp"; // Database for the new web app: WSS_Content_CustomWebApp
builder.DatabaseServer = "localhost"; // Database server name: localhost
builder.UseNTLMExclusively = true; // For using NTLM authentication
builder.DefaultZoneUri = new Uri("CustomUrl");

SPFarmManagedAccountCollection accountCollection = new SPFarmManagedAccountCollection(SPFarm.Local);
SPManagedAccount account = accountCollection.FindOrCreateAccount("SPAdmin account");

builder.ManagedAccount = account; // Used for authentication to Application Pool
builder.ServerComment = webAppProperties.WebAppName;

webApp = builder.Create(); // Creating the web application

webApp.UseClaimsAuthentication = true; // Setting to use Claims Based Authentication
webApp.Name = "CustomWebApplication";

SPIisSettings settings = webApp.IisSettings[SPUrlZone.Default];

settings.AuthenticationMode = System.Web.Configuration.AuthenticationMode.Forms;
settings.EnableClientIntegration = true;

SPFormsAuthenticationProvider authProvider = new SPFormsAuthenticationProvider("UserProvider", "RoleProvider"); // Setting the membership and role providers for Forms auth
((System.Collections.ObjectModel.Collection) settings.ClaimsAuthenticationProviders).Add(authProvider);

SPAlternateUrl internalUrl = new SPAlternateUrl("CustomInternalUrl", SPUrlZone.Default);
webApp.AlternateUrls.Add(internalUrl); // Adding an alternate url

webApp.Update();
webApp.Provision(); // Provisions the application on the local server

If you want to have only forms authentication without windows authentioncation, you should replace the following lines:

SPFormsAuthenticationProvider authProvider = new SPFormsAuthenticationProvider("UserProvider", "RoleProvider"); // Setting the membership and role providers for Forms auth
((System.Collections.ObjectModel.Collection) settings.ClaimsAuthenticationProviders).Add(authProvider);

with the following lines:

List providers = new List();
SPFormsAuthenticationProvider authProvider = new SPFormsAuthenticationProvider("UserProvider", "RoleProvider");
providers.Add(authProvider);
settings.ReplaceClaimsAuthenticationProviders(providers); //this line is the trick

Leave a comment