Skip to content

jeldert/ZNetCS.AspNetCore.Authentication.Basic

 
 

Repository files navigation

ZNetCS.AspNetCore.Authentication.Basic

NuGet

A simple basic authentication middleware.

Installing

Install using the ZNetCS.AspNetCore.Authentication.Basic NuGet package

PM> Install-Package ZNetCS.AspNetCore.Authentication.Basic

Changes in 5.0.0

Added direct references to latest framework and removed no longer supported frameworks. Added possibility to suppress WWWAuthenticate header globally not only on Ajax request.

Important change in 4.0.0

From now assembly is signed.

Important change in 3.0.0

The OnValidatePrincipal will not return AuthenticationResult any more. To simplify process can simply return Task.CompletedTask. Also to make success authentication Principal have to be assigned to ValidatePrincipalContext context.

Usage

When you install the package, it should be added to your .csproj. Alternatively, you can add it directly by adding:

<ItemGroup>
    <PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="5.0.0" />
</ItemGroup>

In order to use the basic authentication middleware, you must configure the services in the Configure and ConfigureServices call of Startup. Because basic authentication is manual process handled on each request, there is need to validate credentials manually (see below).

using ZNetCS.AspNetCore.Authentication.Basic;
using ZNetCS.AspNetCore.Authentication.Basic.Events;
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{   

    // default authentication initialization
    app.UseAuthentication();

    // other middleware e.g. MVC etc
}

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
        .AddBasicAuthentication(
            options =>
            {
                options.Realm = "My Application";
                options.Events = new BasicAuthenticationEvents
                {
                    OnValidatePrincipal = context =>
                    {
                        if ((context.UserName == "userName") && (context.Password == "password"))
                        {
                            var claims = new List<Claim>
                            {
                                new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
                            };

                            var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
                            context.Principal = principal;
                        }
                        else 
                        {
                            // optional with following default.
                            // context.AuthenticationFailMessage = "Authentication failed."; 
                        }

                        return Task.CompletedTask;
                    }
                };
            });
}

or using dependency injection:

public class AuthenticationEvents : BasicAuthenticationEvents
{
    #region Public Methods

    /// <inheritdoc/>
    public override Task ValidatePrincipalAsync(ValidatePrincipalContext context)
    {
        if ((context.UserName == "userName") && (context.Password == "password"))
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
            };

            var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
            context.Principal = principal;
        }

        return Task.CompletedTask;
    }

    #endregion
}

and then registration

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<AuthenticationEvents>();

    services
        .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
        .AddBasicAuthentication(
            options =>
            {
                options.Realm = "My Application";
                options.EventsType = typeof(AuthenticationEvents)
            });
}

As from version 3.0.1 You can suppress the response WWW-Authenticate header (avoiding the browser to show a popup) for ajax requests by using a switch.

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<AuthenticationEvents>();

    services
        .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
        .AddBasicAuthentication(
            options =>
            {
                options.Realm = "My Application";
                options.AjaxRequestOptions.SuppressWwwAuthenticateHeader = true;
            });
}

About

A simple basic authentication middleware.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%