Home Ask Login Register

Developers Planet

Your answer is one click away!

Matthew Verstraete February 2016

Which package lets me query User.Identity in a ASP.Net Core Class Libary?

I am writing a class library for an ASP.Net Core MVC application. In the WebUI package you can directly call into User.Identity to get the Name property of the current user but when I try to access User.Identity in my class library User is recognized but does not have a property called Identity. Because there is almost no documentation I am having a problem figuring out what package to add to the class library so I can query User.Identity for the name of the logged in user. Does anyone know which package I need to ad?


DoctorMick February 2016

User is actually a property of the Controller which is why you wouldn't be able to see if in the same way in your class library. This isn't really going to answer your question but you shouldn't tie your class library to MVC in my opinion, the technology should be isolated to your web project. What you could do it change your class library to accept an IPrincipal parameter (or just a string parameter to just take the name if that's all you need) on the method(s) that needs to get the user's identity and then pass User.Identity from the web site project.

Of course if you're determined to get the user in your class library you can using System.Web.HttpContext.Current.User.

Post Status

Asked in February 2016
Viewed 2,689 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life