Configure the ASP.NET MVC 5 web application to use an external database for user authentication

When a new ASP.NET web application is created with the authentication set to Individual User Accounts, it automatically creates an SQL Express internal database in the App_Data directory of the application when you register the first user. To create the ASP.NET web application with user authentication see Create an ASP.NET Web Application with user authentication

This is sufficient for a small, proof of concept web application, but enterprise-grade applications should use a secure, backed up database.

To connect an ASP.NET MVC web application to an external database

  1. Create a new database in the database server
    CREATE DATABASE MY_DATABASE_NAME
    GO
  2. Create a user and add it to the database as db_owner
    DECLARE @user_name varchar(50)
    SET @user_name = 'MY_USER_NAME'
    
    -- Create the user account
    CREATE LOGIN MY_USER_NAME
     WITH PASSWORD='MY_PASSWORD'
    
    -- Add the user to the database 
    USE MY_DATABASE_NAME; EXEC sp_grantdbaccess @user_name; EXEC sp_addrolemember @rolename = 'db_owner', @membername = @user_name;
    GO
  3. Replace the connection string in the Web.config file
    <connectionStrings>
        <add name="DefaultConnection"
            connectionString="Data Source=MY_SQL_SERVER_NAME;Initial Catalog=MY_DATABASE_NAME;user id=MY_USER_NAME;password=MY_PASSWORD"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

There are two way to generate the user authentication tables

Use the new web application to automatically generate the tables

  1. Run the ASP.NET MVC 5 web application
  2. Select Register in the menu of the web application and register a user
  3. The application will automatically generate the user authentication tables in the database

Generate an SQL script to create the database

Delete the existing database

Only delete the existing database if you have no important data or configuration changes in the existing database!

The script generator checks the database for existing tables, so if you have used the web application to automatically create the tables, delete the database, so the generated script will contain all necessary steps to create the entire database.

  1. Close the tab in Microsoft SQL Server Management Studio that you used to connect to the new database to close the open connection to it
  2. Open a new tab in Microsoft SQL Server Management Studio and execute
    DROP DATABASE MY_DATABASE_NAME

Configure the web application for Code First development.

  1. In the Tools menu of Visual Studio select NuGet Package Manager, Package Manager Console
  2. At the bottom of the screen, the Package Manager Console window opens
  3. In the Package Manager Console execute
    Enable-Migrations –EnableAutomaticMigrations

    PM> Enable-Migrations –EnableAutomaticMigrations
    Checking if the context targets an existing database…
    Code First Migrations enabled for project USC_SocialMediaWebApp.</blockquote

  4. The command generates the Configuration.cs file in the Migrations folder of your solution.

Generate the code to update the database

  1. In the Package Manager Console execute
    Add-Migration Init

    PM> Add-Migration Init
    Scaffolding migration ‘Init’.
    The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running ‘Add-Migration Init’ again.

  2. The command generates the necessary code to create the database tables

If you make changes to your models or the seeding script, execute the “Add-Migration Init” command again in the  Package Manager Console.

Create the database

The steps above generated the necessary code to create the database. To create the database

    1. For the script to be able to create the database, temporarily replace the “db_owner” user with a username and password of a “dbo” user in the database server. Otherwise, we may get

      System.Data.SqlClient.SqlException (0x80131904): CREATE DATABASE permission denied in database ‘master’

      Error Number:262,State:1,Class:14
      CREATE DATABASE permission denied in database ‘master’.

    2. In the Package Manager Console execute
      Update-Database -Verbose

      The -Verbose option displays the executed SQL commands during the database creation. You can save them to create the production database by executing it.

      PM> Update-Database
      Specify the ‘-Verbose’ flag to view the SQL statements being applied to the target database.
      Applying explicit migrations: [201802120745059_Init].
      Applying explicit migration: 201802120745059_Init.
      Running Seed method.

    3. Change the username and password back to the “db_owner” user credentials in the Web.config file of the web application.
    4. Execute this SQL statement in Microsoft SQL Management Studio to add the “db_owner” user to the database
      DECLARE @user_name varchar(50)
      SET @user_name = 'MY_USER_NAME'
      
      -- Add the user to the database 
      USE MY_DATABASE_NAME; EXEC sp_grantdbaccess @user_name; EXEC sp_addrolemember @rolename = 'db_owner', @membername = @user_name;
      GO

Create an ASP.NET MVC 5 Web Application with user authentication

To create a new ASP .NET MVC web application that contains user authentication code

  1. In the File menu of Visual Studio select New, Project
  2. On the left select Visual C#, Web, and in the middle select ASP.NET Web Application (.NET Framework)
  3. Enter the Name, select the solution folder location and click OK. The solution name will automatically match the value of the Name field.
  4. Select MVC and click the Change Authentication button
  5. To use your database for authentication select Individual User Accounts
  6. Make sure the correct authentication is selected and click OK

This ASP.NET MVC 5 web application automatically creates an SQL Express internal database in the App_Data directory of the application when you register the first user.

  1. Start the application
  2. Click Register in the menu of the web application
  3. Register a user

To use an external database for user authentication, see the next post at Configure the ASP.NET MVC 5 web application to use an external database for user authentication

Flashing Side Marker Lamps

In the US it is not required to make the turn signals visible from the side of the vehicle. Many cars sold in the US have their front turn signals positioned between the headlights, so those are usually not visible from the side. If another car is next to yours, the other driver cannot see your rear turn signals, and because there are no front turn signals on the corner of your vehicle, they cannot see the font signals either. You can mount additional turn signals on the side of your car, but most of the time you need to drill holes into the body of the car, that can open the door for early corrosion of your vehicle.

If your car uses conventional light bulbs for the turn signals and the side markers, it is easy to make the side markers flash too.

The idea came from Daniel Stern at http://www.danielsternlighting.com/tech/markerflash/markerflash.html Read the “Crossfeed method” section for the explanation how it works.

Flashing side marker with lights off

Flashing side marker with lights on

The Honda CR-V has plenty of room under the hood to make this modification easy.

  1. Find the side marker and turn signal lights
  2. Disconnect the turn signal connector from the back of the turn signal bulb
  3. Remove the black electrical tape at the junction where the turn signal wires go down to the bulb
  4. Cut the black ground wire with one (1) silver dot leading to the side marker closer to the battery end
  5. Connect the side marker end of the black grounding wire with one (1) silver dot to the wire that goes down to the turn signal. Daniel Stern recommends the Posi-Tap connector to make this connection. The advantage is that you don’t have to cut the turn signal wire, just tap into it. (https://www.allpar.com/reviews/other/posi-tap.html)
  6. I did not have any tapping hardware, so I cut the turn signal wires and used crimp connectors to connect the ground of the side marker bulb to the hot wire of the turn signal.
  7. Temporarily reconnect the turn signal wire to the bulb to test the connection. When the lights are off, the side marker should flash in sync with the turn signal. When the lights are on, the side marker should light up, and when the turn signals operate it should flash too, but in opposite phase. (See videos above)
  8. Disconnect the turn signal wire again to make more room to work, and use a good quality black electrical tape to seal the junctions
  9. Reconnect the turn signal wire.

TeamCity Agent Unmet Requirements

When a TeamCity configuration is created there are requirements the TeamCity Agent has to meet.

The creator of the build configuration can specify Explicit Requirements on the Agent Requirements page of the Build Configuration Settings.

When the build configuration is created, TeamCity automatically adds Build Step Requirements.

The bottom of the same page shows the list of compatible and incompatible agents.

If you visit the incompatible agent, most likely it will not contain the parameter, or the value will be different from the requirement. To see the expected value of the parameter, open the Agent ParametersConfiguration Parameters tab of an agent that satisfies the requirement.

Create a Splunk HTTP Event Collector token

To receive HTTP post messages in Splunk, we need to create a Splunk HTTP Collector token.

To create an index for this token see Create a Splunk index

  1. In the Settings menu select Data Inputs
  2. Select the HTTP Event Collector link
  3. Click the New Token button
  4. Enter a name and description for the token and click the Next button
  5. Select the index to store the events in, set it as the default index, and click the Review button
  6. Click the Submit button to create the token
  7. The confirmation screen shows the token. You will be able to see it again on the token list page. Click the Start Searching button to see the search statement to see the events collected by this token.
  8. You can always search for the events with
    source="http:MY_INPUT" (index="MY_INDEX")

  9. The HTTP Event Collector token list shows the token.

Create a Splunk index

Splunk stores the events in separate databases based on index names. It is faster to search for events if the event types are separated into multiple indexes.

To create an index for the event type you want to send to Splunk

  1. In the Settings menu select Indexes
  2. Click the New Index button
  3. Enter the name of the index. You can leave the default settings, and click the Save button.

Enable the HTTP Event Collector in Splunk

To send events to Splunk via HTTP posts, enable the HTTP Event Collector functionality on the Splunk Enterprise server.

If you operate your own Splunk server

  1. Log into the Splunk web interface as an administrator
  2. In the Settings menu select Data inputs
  3. Select the HTTP Event Collector link
  4. In the upper right corner click the Global Settings button
  5. To enable the Splunk HTTP Event Collector, click the Enabled button
  6. To use HTTP instead of HTTPS uncheck the Enable SSL checkbox
  7. Click the Save button to save the changes

 

How to send an event to the Splunk HTTP Event Collector

The Splunk HTTP Event Collector is the preferred way to send events to Splunk.

The Splunk HTTP Event Collector closes the connection if you don’t use HTTPS when you are sending a POST message.

Recv failure: Connection reset by peer

To send the event from the Macintosh Bash terminal

curl -k https://MY_SPLUNK_SERVER_IP:8088/services/collector -H 'Authorization: Splunk MY_TOKEN' -d '{"sourcetype": "json", "event":"Hello, World!", "host":"MY_HOST_NAME"}'

To send the event from Postman

Turn off the SSL Certificate Validation.

If the Splunk server does not have a trusted SSL certificate, Postman will throw an error if you send the message with the mandatory HTTPS protocol. To tell Postman, you trust the SSL certificate generated by the Splunk server, turn off the SSL certificate validation in Postman.

This option is only available in the standalone version of Postman, not in the Chrome add-on

  1. In the upper right corner of Postman click the wrench
  2. Select Settings
  3. On the General tab turn off SSL certificate verification

Create the post

  1. Add your Splunk token to the header
  2. Add the raw, JSON type body

 

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

When I made changes to the web.config file of an ASP.Net C# application, I have accidentally deleted a comma, and I started to get the runtime error message:

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

Make sure all necessary commas are there in the config files, Visual Studio does not check the format of those lines. In my case, the error was in

<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />

Send Alt-Control-Delete to a Windows server via Remote Desktop

When you log into a Windows server via the Remote Desktop Protocol (RDP) in some cases you need to send the Alt-Control-Delete ( in other notations Alt-Ctrl-Del, Control-Alt-Delete, Ctrl-Alt-Del ) key combination to the remote server.

From a Macintosh laptop

On a Macintosh laptop press the fn+control+option+delete keys to send Alt-Control-Delete to the server.

From a Windows workstation

If the Remote Desktop window is not in full-screen mode, Alt+Control+Delete will control your own Windows workstation.

On a Windows workstation press the Alt+Control+End keys to send Alt-Control-Delete to the remote server.