Can I use the attribute to make .net impersonate another user?

I am familiar with impersonating an account in .net with:

dim myIdentity as WindowsIdentity = someIdentity  
using ctx as WindowsImpersonationContext = myIdentity.Impersonate()  
    doStuff()  
end using

Is it possible to define a .net attribute so that I can write something like:

< runAsUser(someIdentity) > public sub doStuff()

and then when using the method doStuff()?

Update Okay, I was told that this might not be possible, because the arguments to the attribute constructor should be constants that would preclude passing in the user ID.

Let me ask the question a little differently: Suppose that there is a function getUserWindowsIdentity()that returns the identifier associated with the registered user.

< runAsLoggedInUser) > public sub doStuff() >, , getUserWindowsIdentity doStuff()?

+3
5

, . , PostSharp ( .)

, Attribute, , . , .

+1

, .NET framework. , Reflection, . , , .

+2

: a) , doStuff(), b) , doStuff() /, doStuff(), , . ) .

, - http://www.castleproject.org/dynamicproxy/index.html. , /, . , . , , , .

+1

, , , , , , , , . , , :

public class RunAsUserAttribute : Attribute
{
    // implement some attribute functionality here
}

:

[RunAsUser]
public void DoStuff()
{
    // do some stuff as the impersonated user
} 

RunAsUserAttribute? ​​ , soccerdad. .

.

+1

, , , , , , , , .

, , . , script, .

0
source

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


All Articles