Getting user data for each request

My problem is pretty simple - I think. I am running an ASP.NET MVC project. This is a project that requires the user to log in at all times. I probably need current user information in MasterPage, for example; "Howdy, Mark - you're logged in!"

But what if I need the same information in a view? Or some kind of check in my servicelayer?

So, how to make sure that this information is available when I need it, and where do I need it?

+3
source share
6 answers

How much user information do you need? You can always access Thread.Current.Principal and get the username - and perhaps use it to find additional information about the user in the database.

Or, if you really really need some kind of information at any time, you can implement your own basic principle from IPrincipal (it really doesn’t matter!) And add these bits of information there, and when the user logs in, creates an instance of MyCustomPrincipal and attaches it to the current thread. Then it will be available everywhere, everywhere and at any time.

Mark

+2
source

, . , , . , , , , , . , , , , , .

BaseController, , . , .., HttpContext.User.Identity.Name. .

+1

. , .

, , - .

0

ModelBinder.

0

, , , , , ASP.net /.

( ) - , , , ASP.net. membershipProvider RoleProvider msdn, ( , , , !)

PS: Context.Users ! .

0

HttpContext.Current.Users.Identity returns the current user information. Although I'm not sure if it will be passed implicitly when you make a webservice call.

0
source

Source: https://habr.com/ru/post/1712933/


All Articles