ASP.NET Core Web API Authentication

Felix :

I'm struggling with how to set up authentication in my web service. The service is build with the ASP.NET Core web api.

All my clients (WPF applications) should use the same credentials to call the web service operations.

After some research, I came up with basic authentication - sending a username and password in the header of the HTTP request. But after hours of research, it seems to me that basic authentication is not the way to go in ASP.NET Core.

Most of the resources I found are implementing authentication using OAuth or some other middleware. But that seems to be oversized for my scenario, as well as using the Identity part of ASP.NET Core.

So what is the right way to achieve my goal - simple authentication with username and password in a ASP.NET Core web service?

Thanks in advance!

Anuraj :

You can implement a middleware which handles Basic authentication.

public async Task Invoke(HttpContext context)
{
    var authHeader = context.Request.Headers.Get("Authorization");
    if (authHeader != null && authHeader.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
    {
        var token = authHeader.Substring("Basic ".Length).Trim();
        System.Console.WriteLine(token);
        var credentialstring = Encoding.UTF8.GetString(Convert.FromBase64String(token));
        var credentials = credentialstring.Split(':');
        if(credentials[0] == "admin" && credentials[1] == "admin")
        {
            var claims = new[] { new Claim("name", credentials[0]), new Claim(ClaimTypes.Role, "Admin") };
            var identity = new ClaimsIdentity(claims, "Basic");
            context.User = new ClaimsPrincipal(identity);
        }
    }
    else
    {
        context.Response.StatusCode = 401;
        context.Response.Headers.Set("WWW-Authenticate", "Basic realm=\"dotnetthoughts.net\"");
    }
    await _next(context);
}

This code is written in a beta version of asp.net core. Hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Custom authentication asp.net core web api

Web Application and API AzureAD authentication flow ASP.NET Core

ASP.NET Core Web API Facebook JWT Authentication

Google Authentication ASP.NET Core Web Api

ASP.NET Core Web API + Angular 2 Authorization and Authentication

Asp.net Core Web API - Current user & Windows Authentication

JWT Authentication not working in ASP.NEt core web api

Facebook JWT authentication using ASP.NET Core Web API

ASP.Net Core Web API Authentication with Facebook

Using Azure Active Directory authentication in ASP.NET Core 2.0 from Web App to Web API

transfer JWT Authentication implementation from .net core 2 to asp.net web api 2

.Net Core Web API with Client Certificate Authentication

Azure AD Authentication with .NET Core Web API

User Authentication in ASP.NET Web API

ASP.NET Web API with custom authentication

Web API Authentication in ASP.NET 5

authentication with asp.net web api 2

JWT authentication for ASP.NET Web API

Getting 405 error instead of 401 while implementing authentication in ASP.NET Core Web API

could not post to asp.net core web api using windows authentication

Give response to JWT wrong Authentication Asp.Net Core Web Api

Asp.net core web api using windows authentication - Cors request unauthorised

Is it necessary to create tables like AspNetUsers while adding JWT authentication to ASP.NET Core 2.1 Web API?

How to test ASP.NET Core Web API with cookie authentication using Postman?

How to refresh CSRF token on login when using cookie authentication without identity in ASP .NET Core Web API

Are the authentication tokens validated for every request by the ASP.NET Core Web API?

static files - asp.net web api core 2.1 authentication scheme Bearer

How to make email case insensitive authentication in ASP.NET Core Web API login

Consuming Web API secured with JWT authentication from ASP.NET Core MVC application