R
R
Ruslan2019-05-08 18:10:15
ASP.NET
Ruslan, 2019-05-08 18:10:15

How can a C# method "find out" who the current user is if it is used in different types of applications (ASP.NET, Console, WinForms, etc.)?

architectural question.
Suppose there is some class that performs some logic that needs to use the name of the current user. This class can be used in different applications, but the way to get the username in each application is different, for example, in a web application - who is authorized in the application, and in the console - who launched it.
Question: how to architecturally build this class so that it transparently does its job in any type of application?
It comes to my mind to make a dependency on the interface that defines the current user, in each type of application to implement it in its own way, and through dependency injection to inject the necessary dependency into my class directly in any type of application.
Is this approach good? Are there more elegant and correct solutions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Filatov, 2019-05-25
@NYMEZIDE

It comes to my mind to make a dependency on the interface that defines the current user, in each type of application to implement it in its own way, and through dependency injection to inject the necessary dependency into my class directly in any type of application.
Is this approach good ?.

Quite.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question