How to modify default local JWT authentication in dot net core












2















I have jwt authentication in my application,
this is how i implemented in my startup.cs class



services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

});


inside Configure method



app.UseAuthentication();


in controllers used attribute



[Authorize]


Normal authentication works fine.
I want to check some custom things when authenticating without losing the default authentication process, what I mean by that I don't want to write my whole new authentication method.










share|improve this question























  • I guess you need to implement the middleware yourself. This post might be helpful: andrewlock.net/…

    – Tobias Moe Thorstensen
    Jan 2 at 18:12











  • any other way to override?

    – Selik
    Jan 2 at 18:25






  • 1





    Can you describe some custom things in more detail? There are a few ways to approach this, but it depends on what you're trying to achieve.

    – Kirk Larkin
    Jan 2 at 20:58











  • @KirkLarkin I want to read a claim from token and check if it still valid.

    – Selik
    Jan 3 at 6:26











  • well I have implemented it by using filter

    – Selik
    Jan 10 at 6:21
















2















I have jwt authentication in my application,
this is how i implemented in my startup.cs class



services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

});


inside Configure method



app.UseAuthentication();


in controllers used attribute



[Authorize]


Normal authentication works fine.
I want to check some custom things when authenticating without losing the default authentication process, what I mean by that I don't want to write my whole new authentication method.










share|improve this question























  • I guess you need to implement the middleware yourself. This post might be helpful: andrewlock.net/…

    – Tobias Moe Thorstensen
    Jan 2 at 18:12











  • any other way to override?

    – Selik
    Jan 2 at 18:25






  • 1





    Can you describe some custom things in more detail? There are a few ways to approach this, but it depends on what you're trying to achieve.

    – Kirk Larkin
    Jan 2 at 20:58











  • @KirkLarkin I want to read a claim from token and check if it still valid.

    – Selik
    Jan 3 at 6:26











  • well I have implemented it by using filter

    – Selik
    Jan 10 at 6:21














2












2








2


0






I have jwt authentication in my application,
this is how i implemented in my startup.cs class



services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

});


inside Configure method



app.UseAuthentication();


in controllers used attribute



[Authorize]


Normal authentication works fine.
I want to check some custom things when authenticating without losing the default authentication process, what I mean by that I don't want to write my whole new authentication method.










share|improve this question














I have jwt authentication in my application,
this is how i implemented in my startup.cs class



services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

});


inside Configure method



app.UseAuthentication();


in controllers used attribute



[Authorize]


Normal authentication works fine.
I want to check some custom things when authenticating without losing the default authentication process, what I mean by that I don't want to write my whole new authentication method.







c# asp.net-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 18:06









SelikSelik

258




258













  • I guess you need to implement the middleware yourself. This post might be helpful: andrewlock.net/…

    – Tobias Moe Thorstensen
    Jan 2 at 18:12











  • any other way to override?

    – Selik
    Jan 2 at 18:25






  • 1





    Can you describe some custom things in more detail? There are a few ways to approach this, but it depends on what you're trying to achieve.

    – Kirk Larkin
    Jan 2 at 20:58











  • @KirkLarkin I want to read a claim from token and check if it still valid.

    – Selik
    Jan 3 at 6:26











  • well I have implemented it by using filter

    – Selik
    Jan 10 at 6:21



















  • I guess you need to implement the middleware yourself. This post might be helpful: andrewlock.net/…

    – Tobias Moe Thorstensen
    Jan 2 at 18:12











  • any other way to override?

    – Selik
    Jan 2 at 18:25






  • 1





    Can you describe some custom things in more detail? There are a few ways to approach this, but it depends on what you're trying to achieve.

    – Kirk Larkin
    Jan 2 at 20:58











  • @KirkLarkin I want to read a claim from token and check if it still valid.

    – Selik
    Jan 3 at 6:26











  • well I have implemented it by using filter

    – Selik
    Jan 10 at 6:21

















I guess you need to implement the middleware yourself. This post might be helpful: andrewlock.net/…

– Tobias Moe Thorstensen
Jan 2 at 18:12





I guess you need to implement the middleware yourself. This post might be helpful: andrewlock.net/…

– Tobias Moe Thorstensen
Jan 2 at 18:12













any other way to override?

– Selik
Jan 2 at 18:25





any other way to override?

– Selik
Jan 2 at 18:25




1




1





Can you describe some custom things in more detail? There are a few ways to approach this, but it depends on what you're trying to achieve.

– Kirk Larkin
Jan 2 at 20:58





Can you describe some custom things in more detail? There are a few ways to approach this, but it depends on what you're trying to achieve.

– Kirk Larkin
Jan 2 at 20:58













@KirkLarkin I want to read a claim from token and check if it still valid.

– Selik
Jan 3 at 6:26





@KirkLarkin I want to read a claim from token and check if it still valid.

– Selik
Jan 3 at 6:26













well I have implemented it by using filter

– Selik
Jan 10 at 6:21





well I have implemented it by using filter

– Selik
Jan 10 at 6:21












1 Answer
1






active

oldest

votes


















1














You should be able to build on top the default authentication by just chaining authentication schemes.



Firstly, you can implement custom authentication handler:



public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthHandlerOptions>
{
public CustomAuthenticationHandler(IOptionsMonitor<CustomAuthHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
//Write custom logic here
return await Context.AuthenticateAsync(Scheme.Name);
}
}

public class CustomAuthHandlerOptions : AuthenticationSchemeOptions
{
public string MyCustomOptionsProp { get; set; }
}


And then you can add the scheme to the AuthenticationBuilder:



        services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

})
.AddScheme<CustomAuthHandlerOptions, CustomAuthenticationHandler>("CustomAuthJwt", options =>
{
options.MyCustomOptionsProp = "Custom Value";
});


I haven't actually tested this, but I know the idea of this approach works because it has been implemented in the IdentityServer4.AccessTokenValidation Nuget Package. My example is just the most simple version of that.






share|improve this answer
























  • i will check and get back

    – Selik
    Jan 3 at 6:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011111%2fhow-to-modify-default-local-jwt-authentication-in-dot-net-core%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You should be able to build on top the default authentication by just chaining authentication schemes.



Firstly, you can implement custom authentication handler:



public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthHandlerOptions>
{
public CustomAuthenticationHandler(IOptionsMonitor<CustomAuthHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
//Write custom logic here
return await Context.AuthenticateAsync(Scheme.Name);
}
}

public class CustomAuthHandlerOptions : AuthenticationSchemeOptions
{
public string MyCustomOptionsProp { get; set; }
}


And then you can add the scheme to the AuthenticationBuilder:



        services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

})
.AddScheme<CustomAuthHandlerOptions, CustomAuthenticationHandler>("CustomAuthJwt", options =>
{
options.MyCustomOptionsProp = "Custom Value";
});


I haven't actually tested this, but I know the idea of this approach works because it has been implemented in the IdentityServer4.AccessTokenValidation Nuget Package. My example is just the most simple version of that.






share|improve this answer
























  • i will check and get back

    – Selik
    Jan 3 at 6:24
















1














You should be able to build on top the default authentication by just chaining authentication schemes.



Firstly, you can implement custom authentication handler:



public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthHandlerOptions>
{
public CustomAuthenticationHandler(IOptionsMonitor<CustomAuthHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
//Write custom logic here
return await Context.AuthenticateAsync(Scheme.Name);
}
}

public class CustomAuthHandlerOptions : AuthenticationSchemeOptions
{
public string MyCustomOptionsProp { get; set; }
}


And then you can add the scheme to the AuthenticationBuilder:



        services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

})
.AddScheme<CustomAuthHandlerOptions, CustomAuthenticationHandler>("CustomAuthJwt", options =>
{
options.MyCustomOptionsProp = "Custom Value";
});


I haven't actually tested this, but I know the idea of this approach works because it has been implemented in the IdentityServer4.AccessTokenValidation Nuget Package. My example is just the most simple version of that.






share|improve this answer
























  • i will check and get back

    – Selik
    Jan 3 at 6:24














1












1








1







You should be able to build on top the default authentication by just chaining authentication schemes.



Firstly, you can implement custom authentication handler:



public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthHandlerOptions>
{
public CustomAuthenticationHandler(IOptionsMonitor<CustomAuthHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
//Write custom logic here
return await Context.AuthenticateAsync(Scheme.Name);
}
}

public class CustomAuthHandlerOptions : AuthenticationSchemeOptions
{
public string MyCustomOptionsProp { get; set; }
}


And then you can add the scheme to the AuthenticationBuilder:



        services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

})
.AddScheme<CustomAuthHandlerOptions, CustomAuthenticationHandler>("CustomAuthJwt", options =>
{
options.MyCustomOptionsProp = "Custom Value";
});


I haven't actually tested this, but I know the idea of this approach works because it has been implemented in the IdentityServer4.AccessTokenValidation Nuget Package. My example is just the most simple version of that.






share|improve this answer













You should be able to build on top the default authentication by just chaining authentication schemes.



Firstly, you can implement custom authentication handler:



public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthHandlerOptions>
{
public CustomAuthenticationHandler(IOptionsMonitor<CustomAuthHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
//Write custom logic here
return await Context.AuthenticateAsync(Scheme.Name);
}
}

public class CustomAuthHandlerOptions : AuthenticationSchemeOptions
{
public string MyCustomOptionsProp { get; set; }
}


And then you can add the scheme to the AuthenticationBuilder:



        services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};

})
.AddScheme<CustomAuthHandlerOptions, CustomAuthenticationHandler>("CustomAuthJwt", options =>
{
options.MyCustomOptionsProp = "Custom Value";
});


I haven't actually tested this, but I know the idea of this approach works because it has been implemented in the IdentityServer4.AccessTokenValidation Nuget Package. My example is just the most simple version of that.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 18:32









Vidmantas BlazeviciusVidmantas Blazevicius

2,2131419




2,2131419













  • i will check and get back

    – Selik
    Jan 3 at 6:24



















  • i will check and get back

    – Selik
    Jan 3 at 6:24

















i will check and get back

– Selik
Jan 3 at 6:24





i will check and get back

– Selik
Jan 3 at 6:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011111%2fhow-to-modify-default-local-jwt-authentication-in-dot-net-core%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS