Skip to content

Commit

Permalink
Migration of library to .NET Standard 2.1 + sample app to .NET Core 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Kubis committed Apr 22, 2020
1 parent c9b9225 commit bf0f400
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 100 deletions.
2 changes: 1 addition & 1 deletion docker-compose.ci.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.4'

services:
ci-build:
image: microsoft/dotnet:2.2-aspnetcore-runtime
image: mcr.microsoft.com/dotnet/core/aspnet:3.1
volumes:
- .:/src
working_dir: /src
Expand Down
2 changes: 1 addition & 1 deletion samples/IdentitySample.Web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM microsoft/dotnet:2.2-aspnetcore-runtime
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
ARG source
WORKDIR /app
EXPOSE 80
Expand Down
9 changes: 3 additions & 6 deletions samples/IdentitySample.Web/IdentitySample.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>
Expand All @@ -12,10 +11,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CassandraCSharpDriver" Version="3.11.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="CassandraCSharpDriver" Version="3.14.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace IdentitySample.Web.Pages.Admin
{
Expand Down
33 changes: 24 additions & 9 deletions samples/IdentitySample.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AspNetCore.Identity.Cassandra.Extensions;
using IdentitySample.Web.Data;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;

namespace IdentitySample.Web
{
Expand All @@ -15,12 +15,27 @@ public static void Main(string[] args)

public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args).Build();
return WebHost.CreateDefaultBuilder(args)
return new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;

config
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true, reloadOnChange: false);

if (args != null)
config.AddCommandLine(args);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration);
})
.UseStartup<Startup>()
.UseConfiguration(config)
.Build()
.InitializeIdentityDb<ApplicationUser, ApplicationRole>();
.Build();
}
}
}
67 changes: 30 additions & 37 deletions samples/IdentitySample.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using AspNetCore.Identity.Cassandra;
using AspNetCore.Identity.Cassandra;
using AspNetCore.Identity.Cassandra.Extensions;
using IdentitySample.Web.Data;
using IdentitySample.Web.Services;
Expand All @@ -8,57 +7,37 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace IdentitySample.Web
{
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public IConfiguration Configuration { get; private set; }
public IWebHostEnvironment Environment { get; private set; }

public Startup(IHostingEnvironment env)
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();

Configuration = builder.Build();
Configuration = configuration;
Environment = environment;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<CassandraOptions>(Configuration.GetSection("Cassandra"));

services.AddCassandraSession<Cassandra.ISession>(() =>
{
var contactPoints = Configuration
.GetSection("Cassandra:ContactPoints")
.GetChildren()
.Select(x => x.Value);
var cluster = Cassandra.Cluster.Builder()
.AddContactPoints(contactPoints)
.WithCredentials(
Configuration.GetValue<string>("Cassandra:Credentials:UserName"),
Configuration.GetValue<string>("Cassandra:Credentials:Password"))
.Build();
var session = cluster.Connect();
return session;
});
services.AddCassandra(Configuration);

services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddCassandraErrorDescriber<CassandraErrorDescriber>()
.UseCassandraStores<Cassandra.ISession>()
.AddDefaultTokenProviders();

services.AddMvc()
.WithRazorPagesRoot("/Pages")
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
});
services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
});

services.AddAuthentication("myCookie")
.AddCookie("myCookie", options =>
Expand All @@ -69,16 +48,30 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IEmailSender, EmailSender>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
app.UseHttpsRedirection();
}

app.UseAuthentication();
app.UseStaticFiles();
app.UseMvc();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});

app.UseCassandra<ApplicationUser, ApplicationRole>();
}
}
}
21 changes: 13 additions & 8 deletions samples/IdentitySample.Web/appsettings.Production.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
"Default": "Error"
}
},
"CassandraNodes": [
"nosql.data"
],
"CassandraOptions": {
"Cassandra": {
"ContactPoints": [
"nosql.data"
],
"Credentials": {
"UserName": "Cassandra",
"Password": "Cassandra"
},
"KeyspaceName": "identity",
"Replication": {
"class": "NetworkTopologyStrategy",
"datacenter1": "1"
},
"Query": {
"ConsistencyLevel": "One"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Lukas Kubis</Authors>
<Company>Lukas Kubis</Company>
Expand All @@ -11,14 +11,17 @@
<RepositoryUrl>https://github.com/lkubis/AspNetCore.Identity.Cassandra</RepositoryUrl>
<Copyright>Copyright 2017 (c) Lukas Kubis</Copyright>
<Description>Apache Cassandra data store adapter for ASP.NET Core Identity, which allows you to build ASP.NET Core web applications, including membership, login, and user data. With this library, you can store your user's membership related data on Apache Cassandra.</Description>
<Version>2.2.0</Version>
<FileVersion>2.2.0.0</FileVersion>
<Version>2.3.0</Version>
<FileVersion>2.3.0.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CassandraCSharpDriver" Version="3.11.0" />
<PackageReference Include="CassandraCSharpDriver" Version="3.14.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.3" />
<PackageReference Include="Polly" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/AspNetCore.Identity.Cassandra/CassandraOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace AspNetCore.Identity.Cassandra
public class CassandraOptions
{
public List<string> ContactPoints { get; set; }
public int Port { get; set; } = 9042;
public int RetryCount { get; set; } = 3;
public CassandraCredentials Credentials { get; set; }
public string KeyspaceName { get; set; }
public Dictionary<string, string> Replication { get; set; } = null;
Expand Down
2 changes: 1 addition & 1 deletion src/AspNetCore.Identity.Cassandra/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DbInitializer(ISession session)

public void Initialize<TUser, TRole>(CassandraOptions options)
{
if (options == null)
if (options is null)
throw new ArgumentNullException(nameof(options));

if (string.IsNullOrEmpty(options.KeyspaceName))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCore.Identity.Cassandra.Extensions
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseCassandra<TUser, TRole>(this IApplicationBuilder app)
{
using (var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var services = scope.ServiceProvider;
var options = services.GetService<CassandraOptions>();
var initializer = services.GetService<DbInitializer>();

if (initializer is null)
return app;

initializer.Initialize<TUser, TRole>(options);
return app;
}
}
}
}
26 changes: 0 additions & 26 deletions src/AspNetCore.Identity.Cassandra/Extensions/IWebHostExtensions.cs

This file was deleted.

Loading

0 comments on commit bf0f400

Please sign in to comment.