Tuesday, May 5, 2020

Hangfire Dashboard Authentication and Authorization with .Net Framework and OWIN

Used the following the link as a guide

To run the sample solution. Everything is self-contained.
1. Unzip
2. Open solution
3. Clean Solution (Roslyn compiler issue)
4. run

Sample Solution Setup
Created a new project ASP.NET Web Application (.NET Framework)

Enter the name of the project

Choose Single Page Application

Install NuGet package Hangfire

Add local database under App_Data



 Set web.config connectionStrings section to


<connectionStrings>
  <add name="DefaultConnectionconnectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Hangfire.mdf;Initial Catalog=Hangfire;Integrated Security=TrueproviderName="System.Data.SqlClient" />
</connectionStrings>

Add new class HangfireAuthorizationFilter.cs

using Microsoft.Owin;
using Hangfire.Dashboard;
[assembly: OwinStartup(typeof(HangfireDashboardAuth.Startup))]

namespace HangfireDashboardAuth
{
    public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
    {
        public bool Authorize(DashboardContext context)
        {
            if(context == null)
            {
                //may need to add logging that this has happend.
                return false;
            }

            var owinContext = new OwinContext(context.GetOwinEnvironment());
           
            if(owinContext == null ||
                owinContext.Authentication == null ||
                owinContext.Authentication.User == null ||
                owinContext.Authentication.User.Identity == null)
            {
                //may need to add logging that this has happend.
                return false;
            }

            // Allow all authenticated users that have HangfireAdmin role
            return owinContext.Authentication.User.Identity.IsAuthenticated && owinContext.Authentication.User.IsInRole("HangfireAdmin");
        }
    }
}

**nore [assembly: OwinStartup(typeof(HangfireDashboardAuth.Startup))]  this will be different depending what the project name space is.

Change Startup.cs

using Microsoft.Owin;
using Owin;
using Hangfire;

[assembly: OwinStartup(typeof(HangfireDashboardAuth.Startup))]
namespace HangfireDashboardAuth
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
 

            app.UseHangfireDashboard("/hangfire"new DashboardOptions
            {
                Authorization = new[] { new HangfireAuthorizationFilter() }
            });
        }
    }
}

Configure Database
Double click on Hangfire.mdf, there are no tables.


Create Hangfire Tables
(Hangfire auto-creates the Hangfire tables when there are no Hangfire tables present)
To do this start the application (F5), should land on the default page

Navigate to https://localhost:44388/hangfire it will auto redirect back to the login page thanks to the HangfireAuthorizationFilter.
Back at the database, the Hangfire tables have been created.


Create ASP net tables
Try to login


The ASP net tables are now created


Register Users
Click Register

Add the following users for testing
admin@test.com
readonly@test.com

Should now see the new users.


Add Roles
Run the following script

INSERT INTO AspNetRoles (IdNameVALUES ('4f6e6a25-e532-423f-ba60-88bdae3ef6b8''HangfireAdmin')
INSERT INTO AspNetRoles (IdNameVALUES ('f222ed9b-354d-4621-bfaf-8e8d21276ac2''HangfireReadOnly')

Add to AspNetUserRoles association
Associate admin@test.com to the role HangfireAdmin


Test
1.       Not logged in, try to navigate to https://localhost:44388/hangfire will auto redirect to the login page
2.       Login as readonly@test.com, and try to navigate to https://localhost:44388/hangfire
403 Error returned because the user is not associated to HangfireAdmin role.

3.       Login as admin@test.com, and try to navigate to https://localhost:44388/hangfire









Wednesday, December 26, 2018

SSIS

Purpose

Setup a package to use Project parameter connections and deploy to Integrations Services Catalogs

Design

1. Open or Create a new solution

2. Setup the amount of environments in Configuration Manager

clip_image002

clip_image004

3. Open Package and Setup connections. In this case sql db. Right click on Connection Managers

clip_image006

4. Select New and select server and database.

clip_image008

5. Rename connection to something more meaning full ie SQL_Source

clip_image009[4]

To

clip_image010[4]

6. Convert to a project connection.

clip_image012

Name will change from SQL_Source to (project)SQL_Source

clip_image014

7. Parameterize Connection

clip_image016

7.1. Click Ok

clip_image018

double click Project.params to open

clip_image020

8. Add Parameter to Configuration

clip_image022

8.1 Click Add

clip_image024

Now you can adjust the connection strings for each environment

clip_image026

9. You can test that the connection string changes per environment by adjusting the solution configuration

clip_image028

clip_image029[4]

10. Repeat Steps 3-9 for destination connection

clip_image030[4]

clip_image031[4]

11. Lets setup a simple data flow task to test this.

clip_image033

clip_image034

Deployment

1. Skip this step if Integration Services Catalogs has been setup. Setup Integration Services Catalogs.

clip_image035

2. Setup folders in Integration Services Catalogs

clip_image036

4. Deploy package/s for DEV.

clip_image038

Follow the wizard (Please note that a new deployment in project was created in Integration Services Catalogs).

clip_image040

5. Repeat step 4 for test configuration

clip_image041

6. Check configuration

clip_image042

6.1. Dev Environment

clip_image044

6.2. Test Environment

clip_image046

7. Execute Dev package

clip_image047

clip_image048

Result

clip_image050