Get username within startup.cs in Asp.net core












1















I'm using Asp.net Core Authorization and I need to get username within startup.cs but I got null.



public void ConfigureServices(IServiceCollection services)
{
// some code here
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
var svcProvider = services.BuildServiceProvider();
var httpContext = svcProvider.GetService<IHttpContextAccessor>().HttpContext;

entity.AuditData = JsonConvert.SerializeObject(entry);
entity.EntityType = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.AuditUser = httpContext.User.Identity.Name;
})
.IgnoreMatchedProperties(true));
}









share|improve this question

























  • The application startup runs before any other middleware/handler/whatever runs, so whoever is executing the request that starts up the application isn't authenticated yet.

    – CodeCaster
    Nov 20 '18 at 14:41











  • How are you authenticating? Can you get the username from the token?

    – Hubert Jarema
    Nov 20 '18 at 14:55











  • I suppose that AuditEntityAction is not run on startup, but only when audit event occurs - and therefore you do not have to worry to know user in startup code. @Javad do you have any errors with the code?

    – Grzesiek Danowski
    Nov 20 '18 at 15:51













  • I'm using asp.net core authentication and I have no errors. when any insert/update occurs this line entity.AuditUser = httpContext.User.Identity.Name; and codes above run but username is null. idont know how to get username from jwt token if possible.

    – Javad Memariani
    Nov 20 '18 at 16:45











  • I'm using UserManager api of as.net core.

    – Javad Memariani
    Nov 21 '18 at 7:04
















1















I'm using Asp.net Core Authorization and I need to get username within startup.cs but I got null.



public void ConfigureServices(IServiceCollection services)
{
// some code here
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
var svcProvider = services.BuildServiceProvider();
var httpContext = svcProvider.GetService<IHttpContextAccessor>().HttpContext;

entity.AuditData = JsonConvert.SerializeObject(entry);
entity.EntityType = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.AuditUser = httpContext.User.Identity.Name;
})
.IgnoreMatchedProperties(true));
}









share|improve this question

























  • The application startup runs before any other middleware/handler/whatever runs, so whoever is executing the request that starts up the application isn't authenticated yet.

    – CodeCaster
    Nov 20 '18 at 14:41











  • How are you authenticating? Can you get the username from the token?

    – Hubert Jarema
    Nov 20 '18 at 14:55











  • I suppose that AuditEntityAction is not run on startup, but only when audit event occurs - and therefore you do not have to worry to know user in startup code. @Javad do you have any errors with the code?

    – Grzesiek Danowski
    Nov 20 '18 at 15:51













  • I'm using asp.net core authentication and I have no errors. when any insert/update occurs this line entity.AuditUser = httpContext.User.Identity.Name; and codes above run but username is null. idont know how to get username from jwt token if possible.

    – Javad Memariani
    Nov 20 '18 at 16:45











  • I'm using UserManager api of as.net core.

    – Javad Memariani
    Nov 21 '18 at 7:04














1












1








1








I'm using Asp.net Core Authorization and I need to get username within startup.cs but I got null.



public void ConfigureServices(IServiceCollection services)
{
// some code here
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
var svcProvider = services.BuildServiceProvider();
var httpContext = svcProvider.GetService<IHttpContextAccessor>().HttpContext;

entity.AuditData = JsonConvert.SerializeObject(entry);
entity.EntityType = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.AuditUser = httpContext.User.Identity.Name;
})
.IgnoreMatchedProperties(true));
}









share|improve this question
















I'm using Asp.net Core Authorization and I need to get username within startup.cs but I got null.



public void ConfigureServices(IServiceCollection services)
{
// some code here
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
var svcProvider = services.BuildServiceProvider();
var httpContext = svcProvider.GetService<IHttpContextAccessor>().HttpContext;

entity.AuditData = JsonConvert.SerializeObject(entry);
entity.EntityType = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.AuditUser = httpContext.User.Identity.Name;
})
.IgnoreMatchedProperties(true));
}






c# asp.net-core audit.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 8:26







Javad Memariani

















asked Nov 20 '18 at 14:39









Javad MemarianiJavad Memariani

234




234













  • The application startup runs before any other middleware/handler/whatever runs, so whoever is executing the request that starts up the application isn't authenticated yet.

    – CodeCaster
    Nov 20 '18 at 14:41











  • How are you authenticating? Can you get the username from the token?

    – Hubert Jarema
    Nov 20 '18 at 14:55











  • I suppose that AuditEntityAction is not run on startup, but only when audit event occurs - and therefore you do not have to worry to know user in startup code. @Javad do you have any errors with the code?

    – Grzesiek Danowski
    Nov 20 '18 at 15:51













  • I'm using asp.net core authentication and I have no errors. when any insert/update occurs this line entity.AuditUser = httpContext.User.Identity.Name; and codes above run but username is null. idont know how to get username from jwt token if possible.

    – Javad Memariani
    Nov 20 '18 at 16:45











  • I'm using UserManager api of as.net core.

    – Javad Memariani
    Nov 21 '18 at 7:04



















  • The application startup runs before any other middleware/handler/whatever runs, so whoever is executing the request that starts up the application isn't authenticated yet.

    – CodeCaster
    Nov 20 '18 at 14:41











  • How are you authenticating? Can you get the username from the token?

    – Hubert Jarema
    Nov 20 '18 at 14:55











  • I suppose that AuditEntityAction is not run on startup, but only when audit event occurs - and therefore you do not have to worry to know user in startup code. @Javad do you have any errors with the code?

    – Grzesiek Danowski
    Nov 20 '18 at 15:51













  • I'm using asp.net core authentication and I have no errors. when any insert/update occurs this line entity.AuditUser = httpContext.User.Identity.Name; and codes above run but username is null. idont know how to get username from jwt token if possible.

    – Javad Memariani
    Nov 20 '18 at 16:45











  • I'm using UserManager api of as.net core.

    – Javad Memariani
    Nov 21 '18 at 7:04

















The application startup runs before any other middleware/handler/whatever runs, so whoever is executing the request that starts up the application isn't authenticated yet.

– CodeCaster
Nov 20 '18 at 14:41





The application startup runs before any other middleware/handler/whatever runs, so whoever is executing the request that starts up the application isn't authenticated yet.

– CodeCaster
Nov 20 '18 at 14:41













How are you authenticating? Can you get the username from the token?

– Hubert Jarema
Nov 20 '18 at 14:55





How are you authenticating? Can you get the username from the token?

– Hubert Jarema
Nov 20 '18 at 14:55













I suppose that AuditEntityAction is not run on startup, but only when audit event occurs - and therefore you do not have to worry to know user in startup code. @Javad do you have any errors with the code?

– Grzesiek Danowski
Nov 20 '18 at 15:51







I suppose that AuditEntityAction is not run on startup, but only when audit event occurs - and therefore you do not have to worry to know user in startup code. @Javad do you have any errors with the code?

– Grzesiek Danowski
Nov 20 '18 at 15:51















I'm using asp.net core authentication and I have no errors. when any insert/update occurs this line entity.AuditUser = httpContext.User.Identity.Name; and codes above run but username is null. idont know how to get username from jwt token if possible.

– Javad Memariani
Nov 20 '18 at 16:45





I'm using asp.net core authentication and I have no errors. when any insert/update occurs this line entity.AuditUser = httpContext.User.Identity.Name; and codes above run but username is null. idont know how to get username from jwt token if possible.

– Javad Memariani
Nov 20 '18 at 16:45













I'm using UserManager api of as.net core.

– Javad Memariani
Nov 21 '18 at 7:04





I'm using UserManager api of as.net core.

– Javad Memariani
Nov 21 '18 at 7:04












1 Answer
1






active

oldest

votes


















0














I found the problem why httpContext.User.Identity.Name is always empty. for each request to server the header should have Authorization header like below:



Key                Value
Authorization Bearer ZQ0QZFANcPogvA...


without Bearer httpContext is always null. not bad to visit this link






share|improve this answer



















  • 1





    It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

    – Grzesiek Danowski
    Nov 21 '18 at 12:02













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%2f53395425%2fget-username-within-startup-cs-in-asp-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









0














I found the problem why httpContext.User.Identity.Name is always empty. for each request to server the header should have Authorization header like below:



Key                Value
Authorization Bearer ZQ0QZFANcPogvA...


without Bearer httpContext is always null. not bad to visit this link






share|improve this answer



















  • 1





    It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

    – Grzesiek Danowski
    Nov 21 '18 at 12:02


















0














I found the problem why httpContext.User.Identity.Name is always empty. for each request to server the header should have Authorization header like below:



Key                Value
Authorization Bearer ZQ0QZFANcPogvA...


without Bearer httpContext is always null. not bad to visit this link






share|improve this answer



















  • 1





    It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

    – Grzesiek Danowski
    Nov 21 '18 at 12:02
















0












0








0







I found the problem why httpContext.User.Identity.Name is always empty. for each request to server the header should have Authorization header like below:



Key                Value
Authorization Bearer ZQ0QZFANcPogvA...


without Bearer httpContext is always null. not bad to visit this link






share|improve this answer













I found the problem why httpContext.User.Identity.Name is always empty. for each request to server the header should have Authorization header like below:



Key                Value
Authorization Bearer ZQ0QZFANcPogvA...


without Bearer httpContext is always null. not bad to visit this link







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 8:25









Javad MemarianiJavad Memariani

234




234








  • 1





    It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

    – Grzesiek Danowski
    Nov 21 '18 at 12:02
















  • 1





    It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

    – Grzesiek Danowski
    Nov 21 '18 at 12:02










1




1





It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

– Grzesiek Danowski
Nov 21 '18 at 12:02







It depends on the authentication method, for example if you use windows authentication user token is sended as cookie. Simply previously you have anonymous requests.

– Grzesiek Danowski
Nov 21 '18 at 12:02






















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%2f53395425%2fget-username-within-startup-cs-in-asp-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

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?