Skip to content

Commit

Permalink
Renamed basic-auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
ramondeklein committed Jan 17, 2024
1 parent 501b957 commit f0a4f4a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>NWebDav.Sample.Kestrel</AssemblyName>
<Title>NWebDav example using Kestrel</Title>
<Title>NWebDAV example using Basic authentication</Title>
<OutputType>Exe</OutputType>
<Description>WebDAV server sample application (using Kestrel).</Description>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
<PublishSingleFile>true</PublishSingleFile>
<Description>WebDAV server sample application using Basic authentication</Description>
<RootNamespace>NWebDav.Sample.BasicAuth</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
using NWebDav.Server.Authentication;

var builder = WebApplication.CreateBuilder(args);

// Add NWebDAV services and set the options
builder.Services
.AddNWebDav(opts => opts.RequireAuthentication = true)
.AddDiskStore<UserDiskStore>();

// Data protection is used to protect cached cookies. If you're
// not using cached cookies, then data protection is not required
// by NWebDAV.
builder.Services
.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(Path.GetTempPath()));
Expand All @@ -22,11 +27,14 @@
.AddAuthentication(opts => opts.DefaultScheme = BasicAuthenticationDefaults.AuthenticationScheme)
.AddBasicAuthentication(opts =>
{
opts.AllowInsecureProtocol = true;
opts.CacheCookieName = "NWebDAV";
opts.CacheCookieExpiration = TimeSpan.FromHours(1);
opts.AllowInsecureProtocol = true; // This will enable NWebDAV to allow authentication via HTTP, but your client may not allow it
opts.CacheCookieName = "NWebDAV"; // Cache the authorization result in a cookie
opts.CacheCookieExpiration = TimeSpan.FromHours(1); // Cached credentials in the cookie are valid for an hour
opts.Events.OnValidateCredentials = context =>
{
// In a real-world application, this is where you would contact
// you identity provider and validate the credentials and determine
// the claims.
if (context is { Username: "test", Password: "nwebdav" })
{
var claims = new[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.IO;
using System.Security.Authentication;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NWebDav.Server.Stores;

namespace NWebDav.Sample.Kestrel;

public sealed class UserDiskStore : DiskStoreBase
internal sealed class UserDiskStore : DiskStoreBase
{
private readonly IHttpContextAccessor _httpContextAccessor;

Expand All @@ -21,11 +22,17 @@ public override string BaseDirectory
{
get
{
var username = _httpContextAccessor.HttpContext?.User.Identity?.Name;
// Each user has a dedicated directory
var username = User?.Identity?.Name;
if (username == null) throw new AuthenticationException("not authenticated");
var path = Path.Combine(Path.GetTempPath(), username);
Directory.CreateDirectory(path);
return path;
}
}

// Even though the store is a singleton, the HttpContext will still hold
// the current request's principal. IHttpContextAccessor uses AsyncLocal
// internally that flows the async operation.
private ClaimsPrincipal? User => _httpContextAccessor.HttpContext?.User;
}
File renamed without changes.
10 changes: 0 additions & 10 deletions NWebDav.Sample.Kestrel/Namespace.cs

This file was deleted.

2 changes: 1 addition & 1 deletion NWebDav.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Files", "Files", "{942BBBEF
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NWebDav.Server", "NWebDav.Server\NWebDav.Server.csproj", "{6F982556-E0C2-46E9-A90C-7477B3ED19CC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NWebDav.Sample.Kestrel", "NWebDav.Sample.Kestrel\NWebDav.Sample.Kestrel.csproj", "{31CB7C10-0A3F-4760-BDFD-5097046721A6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NWebDav.Sample.BasicAuth", "NWebDav.Sample.BasicAuth\NWebDav.Sample.BasicAuth.csproj", "{31CB7C10-0A3F-4760-BDFD-5097046721A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit f0a4f4a

Please sign in to comment.