HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

External application for the web application deployment

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theapplicationforwebexternaldeployment

Problem

I need an external application that helps deployment of the web application. The application should create the aspnetdb database on the dedicated SQL server. Then it creates the wanted roles for the web application, and it defines the initial users, assigns their roles and properties. The code below works, but it probably needs some cleaning/refactoring (see the questions below the code):

The App.config:


    
        
    
    
        
    
    
        
            
                
                
            
        
        
            
                
                
            
            
               
               
            
        
    


The Program.cs:

```
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web.Management;
using System.Web.Profile;
using System.Web.Security;

namespace InitAspnetdbAppRolesAndUsers
{
class Program
{
static void Main(string[] args)
{
// The connection string of the following name used throughout the application.
const string conStrName = "aspnetdbConnectionString";

// Web application name.
const string webAppName = "web_app";

// Extract the info from the connection string.
string conString =
ConfigurationManager.ConnectionStrings[conStrName].ConnectionString;
SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString);

string SQLServerName = cns.DataSource; //@"COMPUTER\SQL2014INSTANCE";
string SQLServerUser = cns.UserID;
string SQLServerPassword = cns.Password;
string ASPNETdbName = cns.InitialCatalog;

// Create the aspnetdb database. It does not harm if it exists.
SqlServices.Install(SQLServerName,
SQLServerUser, SQLSe

Solution

static void Main(string[] args)


If you're not going to use args, you can omit them:

static void Main()


const string conStrName = "aspnetdbConnectionString";
SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString);


Try not to abbreviate variable names, it makes your code harder to understand.

Also, consider using var, especially if the type of the variable is clear from the right side of the assignment.

string SQLServerName = cns.DataSource;


Local variables should start with a lowercase letter: sqlServerName.

List userList = new List
{
    new[] { "admin", "adminPa$word1234", "admin@example.com", "admin poweruser export", "Jan Admin", "111 222 333" },
    new[] { "admin2", "admin2Pa$word1234", "admin2@example.com", "admin poweruser", "Petr Admin", "222 333 444" },
    new[] { "admin3", "admin3Pa$word1234", "admin3@example.com", "admin", "Lenka Admin", "333 444 555" },
};

foreach (var info in userList)
{
    string name = info[0];
    string password = info[1];
    string email = info[2];


You shouldn't use arrays to store this kind of information, it's logically not an array.

You can use an anonymous type instead:

new { name = "admin", password = "adminPa$word1234", email = "admin@example.com", roleNames = new[] { "admin", "poweruser", "export" }, fullName = "Jan Admin", phone = "111 222 333" }


Note that this means it's much harder to confuse which array index contains which value and you can (and should) use types other than string.

(The incorrect indentation in the original code is due to a bug.)

MembershipCreateStatus status = new MembershipCreateStatus();
memProv.CreateUser(name, password, email,
    "none", // password question
    "none", // password answer
    true,   // isApproved
    null,   //
    out status);


-
You don't need to initialize out variables. (And newing up an enum doesn't make much sense either.)

-
Instead of comments, you should use named arguments, that way, the compiler verifies the names for you (comments can be wrong, code can't).

Put together:

MembershipCreateStatus status;
memProv.CreateUser(name, password, email,
    passwordQuestion: "none",
    passwordAnswer: "none",
    isApproved: true,
    providerUserKey: null,
    status: out status);


status.Equals(MembershipCreateStatus.Success)


You can compare enums using ==:

status == MembershipCreateStatus.Success

Code Snippets

static void Main(string[] args)
static void Main()
const string conStrName = "aspnetdbConnectionString";
SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString);
string SQLServerName = cns.DataSource;
List<string[]> userList = new List<string[]>
{
    new[] { "admin", "adminPa$$word1234", "admin@example.com", "admin poweruser export", "Jan Admin", "111 222 333" },
    new[] { "admin2", "admin2Pa$$word1234", "admin2@example.com", "admin poweruser", "Petr Admin", "222 333 444" },
    new[] { "admin3", "admin3Pa$$word1234", "admin3@example.com", "admin", "Lenka Admin", "333 444 555" },
};

foreach (var info in userList)
{
    string name = info[0];
    string password = info[1];
    string email = info[2];

Context

StackExchange Code Review Q#107757, answer score: 6

Revisions (0)

No revisions yet.