Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The example is almost fully from the default Visual Studio template for a Razor

### Framework

NET Core 6.0
NET Core 9.0

### Template modifications

Expand All @@ -30,7 +30,7 @@ The example application applies the steps described for the [Castle Baseline Int
- `Startup.cs` Ioc
- `Areas/Identity/Pages/Account/Login.cshtml.cs` Castle SDK calls
- `Pages/Shared/_Layout.cshtml` Client-side Castle
- `appsettings.json` Your Castle API secret and App ID
- `appsettings.json` Your Castle API secret and Test Request Token

## Development testing

Expand Down
16 changes: 7 additions & 9 deletions src/CastleDemo/Areas/Identity/Pages/Filter/Filter.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


using Castle;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -10,18 +9,22 @@
using System.Threading.Tasks;
using System;
using Castle.Infrastructure.Exceptions;
using Microsoft.Extensions.Configuration;

namespace CastleDemo.Areas.Identity.Pages.Filter
{
[AllowAnonymous]
public class FilterModel : PageModel
{

private readonly IConfiguration _configuration;
private readonly CastleClient _castleClient;
public FilterModel(
CastleClient castleClient)
CastleClient castleClient, IConfiguration configuration
)
{
_castleClient = castleClient;
_configuration = configuration;
}

private string SubmitBtn { get; set; }
Expand Down Expand Up @@ -80,14 +83,9 @@ private ActionRequest CreateCastleActionRequest(string castleEvent)
User = user,
Changeset = changeset,
Properties = properties,
RequestToken = "vru9P7qwelbdOiFyGPavoPdJNX9_7mTjsOtQ-2ewrl8qFLNILm_HcXtLrQwMmDNYExujcTMqu2IhWLNYZB3OA-K45FlZjLFUdBOzI0-b_jNRdt8wSjCGchs_mwtCcdczXGyTEn8_gmwFL4h8fHbdah8kkyQdK5p8am_DME5I1j5gdsdzHiyEchgpk3RgV-cRZzOTMEJ01nxsetA3RDaTH0Nt3DFOMIpqBS-daB0ph3IaLoN8eH7VPVl2nGkYKJ1vHT_2OEwwimoFL51tGyqHch0t31RKK4Q-TyyFbFwctNcri7s5Ty_WPR150MB9Xv0bZ1qTdGJxxzlHM5MVRWvWMANNmnxjW5MbWX7DNEJ8wHwdLIN8b3bBOUhrgBgaLpMqWECGAxs_wy90KuxsBz_3b28ugnEZKZ1uGzGCbBsxhG8ZKpr4Py-CcxsunG0SKINwCy-CZhsviWwbsLtcnj526q_Us48qwjAozB9YX9xS3FgPYkxeY9ezwSupCJ8ryHTojNcN44mqEupSq8zmSaXRGm1fd1wrH7OxxvJescZ35zR_d9fmkXTYGW77Pwy1F79cLR-zfhrg",
RequestToken = _configuration["Castle:TestRequestToken"],
Context = Castle.Context.FromHttpRequest(Request)
};
}
}





}
}
9 changes: 7 additions & 2 deletions src/CastleDemo/Areas/Identity/Pages/Log/Log.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.VisualStudio.Threading;
using System.Threading.Tasks;
using System;
using Microsoft.Extensions.Configuration;

namespace CastleDemo.Areas.Identity.Pages.Log
{
Expand All @@ -17,10 +18,14 @@ public class LogModel : PageModel
{

private readonly CastleClient _castleClient;
private readonly IConfiguration _configuration;
public LogModel(
CastleClient castleClient)
CastleClient castleClient,
IConfiguration configuration
)
{
_castleClient = castleClient;
_configuration = configuration;
}

public async Task<ActionResult> SendLog()
Expand Down Expand Up @@ -64,7 +69,7 @@ private ActionRequest CreateCastleActionRequest(string castleEvent)
User = user,
Properties = properties,
AuthenticationMethod = authMethod,
RequestToken = "vru9P7qwelbdOiFyGPavoPdJNX9_7mTjsOtQ-2ewrl8qFLNILm_HcXtLrQwMmDNYExujcTMqu2IhWLNYZB3OA-K45FlZjLFUdBOzI0-b_jNRdt8wSjCGchs_mwtCcdczXGyTEn8_gmwFL4h8fHbdah8kkyQdK5p8am_DME5I1j5gdsdzHiyEchgpk3RgV-cRZzOTMEJ01nxsetA3RDaTH0Nt3DFOMIpqBS-daB0ph3IaLoN8eH7VPVl2nGkYKJ1vHT_2OEwwimoFL51tGyqHch0t31RKK4Q-TyyFbFwctNcri7s5Ty_WPR150MB9Xv0bZ1qTdGJxxzlHM5MVRWvWMANNmnxjW5MbWX7DNEJ8wHwdLIN8b3bBOUhrgBgaLpMqWECGAxs_wy90KuxsBz_3b28ugnEZKZ1uGzGCbBsxhG8ZKpr4Py-CcxsunG0SKINwCy-CZhsviWwbsLtcnj526q_Us48qwjAozB9YX9xS3FgPYkxeY9ezwSupCJ8ryHTojNcN44mqEupSq8zmSaXRGm1fd1wrH7OxxvJescZ35zR_d9fmkXTYGW77Pwy1F79cLR-zfhrg",
RequestToken = _configuration["Castle:TestRequestToken"],
Context = Castle.Context.FromHttpRequest(Request)
};
}
Expand Down
35 changes: 25 additions & 10 deletions src/CastleDemo/Areas/Identity/Pages/Risk/Risk.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace CastleDemo.Areas.Identity.Pages.Risk
{
Expand All @@ -16,19 +17,28 @@ public class RiskModel : PageModel
{

private readonly CastleClient _castleClient;
private readonly IConfiguration _configuration;
public RiskModel(
CastleClient castleClient)
CastleClient castleClient,
IConfiguration configuration)
{
_castleClient = castleClient;
_configuration = configuration;
}

public async Task<ActionResult> SendRisk()
{

var res = await _castleClient.Risk(CreateCastleActionRequest("$profile_update"));

return Page();

try
{
var request = CreateCastleActionRequest("$profile_update");
var res = await _castleClient.Risk(request);
return Page();
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.ToString());
return Page();
}
}

private ActionRequest CreateCastleActionRequest(string castleEvent)
Expand All @@ -43,14 +53,18 @@ private ActionRequest CreateCastleActionRequest(string castleEvent)
user.Add("name", "some random name");
user.Add("traits", traits);



var product = new Dictionary<string, string>();
product.Add("id", "12345");

var properties = new Dictionary<string, object>();
properties.Add("some prop", 1234);


// Create a custom context with a public IP address
var context = Castle.Context.FromHttpRequest(Request);
// Override the IP address for testing purposes


return new ActionRequest()
{
Event = castleEvent,
Expand All @@ -59,8 +73,9 @@ private ActionRequest CreateCastleActionRequest(string castleEvent)
Product = product,
User = user,
Properties = properties,
RequestToken = "vru9P7qwelbdOiFyGPavoPdJNX9_7mTjsOtQ-2ewrl8qFLNILm_HcXtLrQwMmDNYExujcTMqu2IhWLNYZB3OA-K45FlZjLFUdBOzI0-b_jNRdt8wSjCGchs_mwtCcdczXGyTEn8_gmwFL4h8fHbdah8kkyQdK5p8am_DME5I1j5gdsdzHiyEchgpk3RgV-cRZzOTMEJ01nxsetA3RDaTH0Nt3DFOMIpqBS-daB0ph3IaLoN8eH7VPVl2nGkYKJ1vHT_2OEwwimoFL51tGyqHch0t31RKK4Q-TyyFbFwctNcri7s5Ty_WPR150MB9Xv0bZ1qTdGJxxzlHM5MVRWvWMANNmnxjW5MbWX7DNEJ8wHwdLIN8b3bBOUhrgBgaLpMqWECGAxs_wy90KuxsBz_3b28ugnEZKZ1uGzGCbBsxhG8ZKpr4Py-CcxsunG0SKINwCy-CZhsviWwbsLtcnj526q_Us48qwjAozB9YX9xS3FgPYkxeY9ezwSupCJ8ryHTojNcN44mqEupSq8zmSaXRGm1fd1wrH7OxxvJescZ35zR_d9fmkXTYGW77Pwy1F79cLR-zfhrg",
Context = Castle.Context.FromHttpRequest(Request)
// https://docs.castle.io/docs/test-tokens for more info
RequestToken = _configuration["Castle:TestRequestToken"],
Context = context
};
}
}
Expand Down
1 change: 0 additions & 1 deletion src/CastleDemo/Areas/Identity/Pages/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@using Microsoft.AspNetCore.Identity
@using CastleDemo.Areas.Identity
@using Microsoft.AspNetCore.Identity
@namespace CastleDemo.Areas.Identity.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
25 changes: 12 additions & 13 deletions src/CastleDemo/CastleDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<UserSecretsId>aspnet-CastleDemo-708B0CB0-0EAA-415A-87AA-60C6A98D774C</UserSecretsId>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.Sdk" Version="2.1.1"/>
<PackageReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.0.102"/>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.13"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.13"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.13"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.13"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.13"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.13"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.13"/>
<PackageReference Include="Castle.Sdk" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.0.102" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.13" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions src/CastleDemo/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
</environment>
<link rel="stylesheet" href="~/css/site.css" />

<!-- Castle client side fingerprinting -->
<script src="@("https://d2t77mnxyo7adj.cloudfront.net/v1/c.js?" + Configuration["Castle:AppId"])"></script>

<!-- Update Castle's client side with user details -->
@if (SignInManager.IsSignedIn(User))
Expand Down
94 changes: 75 additions & 19 deletions src/CastleDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,80 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Castle;
using Castle.Config;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using CastleDemo.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Identity.UI;

namespace CastleDemo
var builder = WebApplication.CreateBuilder(args);

// -- Services --
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("CastleDemo"));

builder.Services.AddIdentityCore<IdentityUser>(options =>
{
// Configure identity options
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 1;
})
.AddDefaultUI()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

// Add authentication services
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();

builder.Services.AddControllersWithViews();

// Castle configuration
var castleConfig = new CastleConfiguration(builder.Configuration["Castle:ApiSecret"]);

builder.Services.AddSingleton(new CastleClient(castleConfig));

var app = builder.Build();

// -- Middleware Pipeline --
if (app.Environment.EnvironmentName != "Development")
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
else
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

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

app.MapDefaultControllerRoute();
app.MapRazorPages();

app.Run();
72 changes: 0 additions & 72 deletions src/CastleDemo/Startup.cs

This file was deleted.

Loading