Question

I would like to pass certain data from an instance of an object to its attribute, and I have troubles inderstanding how to implement it. Here's a sample:

[AuthenticateAttribute]
public class MyController: Controller
{
    UserInfo info;
}

The idea is that AuthenticateAttribute instance would populate the UserInfo instance.

I want to pass reference to info to an instance of AuthenticateAttribute and thereby eliminate strong coupling between the attribute and the particular MyController class.

Is this possible?

Was it helpful?

Solution

The attribute itself shouldn't have the UserInfo field IMO. Bear in mind that there will be one instance of the attribute for the type it's applied to - not one per instance of that type.

If you could give a fuller example (showing the code it's applied to) we may be able to help more...

OTHER TIPS

The ViewData collection of the controller is accessible from the attribute and you can add your UserInfo object to it and then access it in the controller's acions. You can also use the a typed model containing UserInfo. This scenario is described here Get permission from Authorize Attribute?

Changing type fields in attribute that is applied to it is not common and you have to be very cautious.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top