andreas
andreas

Reputation: 93

Custom AuthorizeAttribute - Endpoint not work

I'm very new to Asp.net Core, i hope someone can help to find the problem. I have Created a Custom Authorize Attribute to check login data stored in appsettings.json The Authorize Attribute work (breakpoints hit, data correct). But after the check, the requested endpoint never hit.

I have tested if the endpoint is work without authorization. And yes it's work.

In Startup.cs i load my Logins from appsettings.json and add it to the service as singleton.

LoginModel[] logins = Configuration.GetSection("LoginUsers").Get<LoginModel[]>();
Settings setting = new Settings();
setting.LoginModels = logins;
services.AddSingleton(setting);

My AuthorizeAttribute:


[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class MyAuthorizeAttribute : AuthorizeAttribute, IAuthorizationFilter
{

    public MyAuthorizeAttribute()
    {
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
        // get username and password from header
        string authHeader = context.HttpContext.Request.Headers["Authorization"];
        string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
        Encoding encoding = Encoding.GetEncoding("iso-8859-1");
        string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
        int seperatorIndex = usernamePassword.IndexOf(':');
        var username = usernamePassword.Substring(0, seperatorIndex);
        var password = usernamePassword.Substring(seperatorIndex + 1);

        var services = context.HttpContext.RequestServices;
        var settings = services.GetService<Settings>();

        var loginfound = settings.LoginModels.Where(x => x.Username == username && x.Password == password).FirstOrDefault();


        if (loginfound == null)
        {
            context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
        }
    }
}

My Controller:

[Route("api/[controller]")]
[ApiController]
[MyAuthorize]
public class SettingsController : ControllerBase
{

    [HttpGet("checkLogin")]
    public IActionResult CheckLogin()
    {

        return Ok(true);
    }
}

My endpoint is not work anymore. What am I doing wrong? I dont have any errors.

Upvotes: 0

Views: 502

Answers (1)

Stephen McDowell
Stephen McDowell

Reputation: 959

For using your own authorize logic with IAuthorizationFilter, you should not use with AuthorizeAttribute which will check the Authentication with default authentication schema.

Reference Tao Zhou's answer here: Asp.Net Core 2.1 - Authorize based on content in request

Upvotes: 1

Related Questions