AuthorizationAttribute IsAuthorized not called - c#

I've created a custom attribute that derives from System.ComponentModel.DataAnnotations.AuthorizationAttribute and overrides the IsAuthorized method.
I've decorated my methods with the new attribute but the IsAuthorized method is never called. Can anyone explain why?
Here's my attribute declaration :
[AttributeUsage(AttributeTargets.All)]
public class AuthorisationAttribute : System.ComponentModel.DataAnnotations.AuthorizationAttribute
{
private bool IsAuthorised { get; set; }
public AuthorisationAttribute(bool isAuthorised)
{
IsAuthorised = isAuthorised;
}
protected override AuthorizationResult IsAuthorized(IPrincipal principal, AuthorizationContext authorizationContext)
{
return IsAuthorised ? AuthorizationResult.Allowed : new AuthorizationResult("You are not authorised for this activity");
}
}
and here is my usage :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[Authorisation(true)]
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Pass");
}
[Authorisation(false)]
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Shouldn't see this");
}
}
Thanks in advance.

With very few exceptions, attributes aren't injection points. They don't do anything at all by themself. They are just metadata (information about types / members). For them to be active, you need the calling framework to explicitly check for them (via reflection), and invoke any methods.
This is the case in some places; MVC uses attributes extensively, for example. But this is not the case for winform event-handlers and other arbitrary methods. So nothing happens.
Basically, you've done the equivalent of putting a sticky label with "do not enter" onto the door, but in a place that nobody can see by default (the top edge of the door, for example). It only does something if (both):
somebody looks for the label
people either electively obey the label, or you have an explicit door-person who checks the label against the people going in/out

Related

switch usercontrols in mainform within a usercontrol click event

This may sound stupid, But I am having hard time to figure this out; any help would be appreciated:
I have two user controls called “Safety_Check” and “OEE_Track”. In my MainForm I have a panel called “pnl_main_controller” this is where I am displaying both my user controls. I have two buttons on my main form and I am dynamically switching between both without any issue.
Safety_Check User control;
public partial class Safety_Check : UserControl
{
private static Safety_Check _instance;
public static Safety_Check instance
{
get
{
if (_instance == null)
_instance = new Safety_Check();
return _instance;
}
}
public Safety_Check()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
///////------------------------
}
}
OEE_Track User control
public partial class OEE_Track : UserControl
{
private static OEE_Track _instance;
public static OEE_Track instance
{
get
{
if (_instance == null)
_instance = new OEE_Track();
return _instance;
}
}
public OEE_Track()
{
InitializeComponent();
}
}
MainForm:
public partial class MainForm : Form
{
private void btn_reg_Click(object sender, EventArgs e)
{
if (!pnl_main_controller.Contains(Safety_Check.instance))
{
pnl_main_controller.Controls.Add(Safety_Check.instance);
Safety_Check.instance.Dock = DockStyle.Fill;
Safety_Check.instance.BringToFront();
}
else
{
Safety_Check.instance.BringToFront();
} }
private void btn_OEE_Click(object sender, EventArgs e)
{
if (!pnl_main_controller.Contains(OEE_Track.instance))
{
pnl_main_controller.Controls.Add(OEE_Track.instance);
OEE_Track.instance.Dock = DockStyle.Fill;
OEE_Track.instance.BringToFront();
}
else
{
OEE_Track.instance.BringToFront();
}
}
What I am trying to do is I have a button called “Button1” on my “Safety_Check” Usercontrol, whenever I press this , I want “Safety_Check” to be disappear on “pnl_main_controller” and bring “OEE_Track” to the panel
There are several solutions for interaction between controls. Controls are classes and like any other class they can interact with each other using their public properties and methods or using some mediator.
In this case, your controls don't need to know each other and don't need to interact to each other directly:
They can ask another object which knows both controls, to do the job for them.
Or they can raise their request notification and the one who subscribed to that notification, will serve it.
To ask another object to do the job for them you have multiple solutions. As an example you can implement a specific interface in the parent form and in the child controls, cast the parent to that specific interface and call a specific method which do the job for you.
For raising the request notification, an easy solution is relying on events. You can create an event in the child control and raise it when you need the parent do something for you. Then in the parent subscribe to that event and do the job.
Example - Using event
I assume you have UserControl1 having Button1 inside and you have handled Click event of Button1. Then you can create Button1Clicked event and raise it when Button1 clicked:
public event EventHandler Button1Clicked;
private void Button1_Click(object sender, EventArgs e)
{
Button1Clicked?.Invoke(this, e);
}
Then in the parent form, subscribe for the event and do whatever you want:
private void userControl11_Button1Clicked(object sender, EventArgs e)
{
//Hide userControl11 and show userControl21
}
Example - Using interface
I assume, you have an interface having a few standard methods:
public interface IDoSomething
{
void DoSomething();
void DoSomethingElse();
}
And you have implemented the interface in your parent form:
public class Form1: Form, IDoSomething
{
// ...
public void DoSomething()
{
//Hide userControl11 and show userControl21
}
public void DoSomethingElse()
{
// ...
}
}
Then in you user control:
private void Button1_Click(object sender, EventArgs e)
{
var f = FindForm() as IDoSomething;
if(f!=null)
f.DoSomething();
}
I want to expand on Reza Aghaei's answer a bit. I think it could get even better than it is now.
First way to do this
If I were you I would have some interface ICheckedView which has at least 1 method to implement like so:
ICheckedView
{
void Continue();
}
Now, we're able to apply this interface to any class in our solution, most likely to views, though. Next, I would make your main form implement this interface and implement the required method. The in this case we want our main form to remove the control from the panel and add a new control. Frankly, our Safety check control doesn't need to (and maybe shouldn't) know about other controls or what happens next. It's just used for flow of control.
Finally, you need to add either a public property, or maybe even a parameter to the constructor for Safety_Check which includes an ICheckedView in it. When your safety check control gets clicked it can tell whoever has been passed into it (we'll say the client) that it must continue.
Second way to do this
It can be done with an action delegate.
If you add an Action delegate to your safety check, you could just pop in any method whose signature matches that delegate (void methodName()). The constructor for your Safety_Check control should include an Action and that Action would get assigned to a private field of the class. Then when it's time to invoke, that action can be invoked directly.
Notes on this method
Because we're probably invoking from the UI thread in the first place, we're probably alright, but you need to think about thread safety here. The invoke required pattern can help you around this.

Is this efficient and should I be using encapsulation in a better way?

Here is the code. Please see my questions at the bottom of this post.
public partial class myClass : Other.Class
{
long check1parameter = CurrentSession.CurrentFile.ID;
protected override void EnquiryLoaded(object sender, System.EventArgs e)
{
disableFields();
}
private void disableFields()
{
if (checkEverything()) {
EnquiryForm.GetControl("Status").Enabled = true;
}
}
public bool check1_method(long check1parameter) {
bool Check1 = false;
string stringToCheck = check1parameter.ToString();
if (stringToCheck.Contains("something")) {
Check1 = true;
}
return Check1;
}
public bool checkEverything() {
bool roleCheck = CurrentSession.CurrentUser.IsInRoles("RequiredRole");
bool check1 = check1_method(check1parameter);
bool checkEverything = false;
if (roleCheck && check1) {
checkEverything = true;
}
return checkEverything;
}
//other methods
}
The code is to check that someone has a role and also that a string contains a bit of info and then disable a field. I have simplified this from the actual code to outline the key points. Although the intention is only to run these simple checks and disable a field, I thought it best to create individual methods for these tasks so they can be expanded later on.
I do get an object reference error with long check1parameter being defined in that position. It was in check1_method() and worked correctly but it's something I'd like to be declared once and used across multiple areas if possible.
I also want to pass parameters\variables to check1_method rather than declaring them inside it. What's the best way to approach making check1parameter available to all methods in this partial class? It refers to another class which is linked to Other.Class in some way.
My main question is - how do I make this as efficient as possible and should I be using private in place of public anywhere here? I'm still very new to C# and haven't quite figured out encapsulation yet, so please go easy on me! :)
myClass doesn't need to be declared as partial unless you intend to continue implementing it in a different file.
When using a simple if statement they can be removed, for example you could write:
public partial class myClass : Other.Class
{
long check1parameter = CurrentSession.CurrentFile.ID;
protected override void EnquiryLoaded(object sender, System.EventArgs e)
{
disableFields();
}
private void disableFields()
{
EnquiryForm.GetControl("Status").Enabled = checkEverything();
}
public bool check1_method(long check1parameter) {
return check1parameter.ToString().Contains("something");
}
public bool checkEverything() {
bool roleCheck = CurrentSession.CurrentUser.IsInRoles("RequiredRole");
bool check1 = check1_method(check1parameter);
return (roleCheck && check1);
}
//other methods
}
In order to save yourself from declaring unnecessary bools. Beyond that you'd be sacrificing readability for fewer lines.
When it comes to public vs private, it's good practice to always specify private unless you need to access it from outside of the class. At a glance, disableFields() should probably be public, and check1_method() and checkEverything() be private.
EDIT:
Also, if check1parameter is instantiated globally to myClass, then you don't need to pass it in as a parameter to check1_methods()
The code you provided looks ok. I've made a couple of changes, mostly code aesthetics. The main one is to make the 2 check methods into properties.
public partial class myClass : Other.Class
{
long check1parameter = CurrentSession.CurrentFile.ID;
protected override void EnquiryLoaded(object sender, System.EventArgs e)
{
disableFields();
}
private void disableFields()
{
if (checkEverything)
{
EnquiryForm.GetControl("Status").Enabled = true;
}
}
// the parameter name was the same as a variable in the class
// renamed to avoid confusion
public bool check1_method
{
get {return check1parameter.ToString().Contains("something");}
}
public bool checkEverything
{
get { return CurrentSession.CurrentUser.IsInRoles("RequiredRole")
&& check1_method; }
}
//other methods
}

Is it normal practice to use classes inherited from EventArgs as parameters

Is it normal practice to use classes inherited from EventArgs as parameters in methods or i should make Interface.
For example:
public class EvArgsTest : EventArgs
{
public string Test { get { return "test"; } }
}
public foo()
{
zoo(new EvArgsTest());
}
public zoo(EvArgsTest tr)
{
Console.WriteLine(EvArgsTest.Test());
}
Is it good style?
(Sure i in case if i am already using EvArgsTest for events)
This is really a bad practice!
EventArgs are meant to be use with events.
From MSDN
EventArgs class represents the base class for classes that contain event data, and
provides a value to use for events that do not include event data.
I don't understand why you need to pass an object that inherit EventArgs to a method..
Create your own class or add more argments to methods

Multiple inheritance in C# - again

I know that C# does not offer multiple inheritance. And I know there' are workarounds like this one for instance.
But here's a problem that I faced today, can't figure any ELEGANT workaround. I'll add some abstract code-sample so you get it quicker...
(let it be a real-life ASP.NET code - cause those "class A, class B" code-samples are really confusing):
public class AdminPage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
//if not an admin - get out
if(!CurrentUserIsAdmin()) Response.End();
base.OnInit (e);
}
}
public class JQueryPage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
RegisterJQueryScript();
base.OnLoad (e);
}
}
//now here's what I REALLY miss in C#
public class AdminJQueryPage : AdminPage, JQueryPage;
Compose out the functionality? This is better for Single Responsibility. You'd have to think carefully about your constructors.
interface IAdminPage {
public string AdminPageMethod();
}
interface IJQueryPage {
public string JQueryPageMethod();
}
internal class AdminPage : IAdminpage {
private string someString;
internal AdminPage(string value) {
this.someString = value;
}
public string AdminPageMethod() {
return "AdminPage result with some string: " + this.someString;
}
}
internal JQueryPage : IJQueryPage {
private int someNumber;
internal JQueryPage(int value) {
this.someNumber = value;
}
public string JQueryPageMethod() {
return "JQueryPage result with number: " + this.someNumber;
}
}
class AdminJQueryPage : IQueryPage, IAdminpage {
private readonly IAdminPage adminPage;
private readonly IJQueryPage jqueryPage;
public AdminJQueryPage(string someString, int someNumber) {
this.adminPage = new AdminPage(someString);
this.jqueryPage = new JQueryPage(someNumber);
}
public string AdminPageMethod() {
return this.adminPage.AdminPageMethod();
}
public string JQueryPageMethod() {
return this.adminPage.JQueryPageMethod();
}
}
If you really want multiple inheritance, look at Scala's traits
Edit: added passing of constructor values to composed out classes. Also made the classes internal (cannot be accessed or constructed outside the assembly) because they are only ever constructed by the AdminJQueryPage class, which is the 'public-facing' class.
I came from C++ too and dont miss it, especially since reading Refactoring [and using a non-OOTB tool for that].
You can use PostSharp to post process based on placing attributes on your AdminJQueryPage which would achieve the exact same effect.
Or you can Extract Method code into helper classes and call that (i.e., Joe's example)
Or you can put the helpers in a single base class and call from that.
Either way your code will be clearer.
It's only a matter of time before your mixins start overlapping, and then your general suite of techniques for managing that complexity needs to kick in - in C++, MI should only have been one tool in a suite - rather than a very sexy hammer.
its possible to fake a mixin by specifying a interface and creating extension methods for that interface. however I'm not use this will help overriding methods, only adding new ones. you are of course able to then call an extension method when overriding, but that is basically the same as extracting the methods to a helper class, but with a little more sugar
Even if it was possible, one problem with the semantics of an MI-based solution to the specific problem you raised is what happens on the markup side? The Render() method that generates the markup would run first in one class, and then in the other? That's probably not the behavior you want when both classes generate entire pages.
If you're open to solutions that are outside of the language itself, there are several elegant options in ASP.NET that will address the type of issue you raised (changing the actions taken during an event in the page life cycle). For example:
Page Adapters
Control Adapters
Custom user controls
HttpModules
Master Pages
Tag mapping
The best choice will of course depend on the details of your application. In case it's helpful, I cover those options in my book, including sample code: Ultra-Fast ASP.NET.
The simplest approach is to build a hierarchy - allow AdminPage to inherit from JQueryPage like so:
public class AdminPage : JQueryPage
{
protected override void OnInit(EventArgs e)
{
//if not an admin - get out
if(!CurrentUserIsAdmin()) Response.End();
base.OnInit (e);
}
}
public class JQueryPage : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
RegisterJQueryScript();
base.OnLoad (e);
}
}
//now here's what I REALLY miss in C#
public class AdminJQueryPage : AdminPage
My guess is some of this awkwardness comes from the ASP.NET page model, which uses overridden base class methods.
You can to do this with Interfaces
public interface IJQueryPage
{
}
public abstract class AdminPage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
//if not an admin - get out
if(!CurrentUserIsAdmin()) Response.End();
base.OnInit (e);
}
protected override void OnLoad(EventArgs e)
{
if (this is IJQueryPage)
{
RegisterJQueryScript();
}
base.OnLoad (e);
}
}
public class AdminJQueryPage : AdminPage, IJQueryPage
{
}

How to call an EventHandler in a parent class

I have added an EventHandler for the Click-event to a picturebox but on runtime this handler is never called (the debugger shows me that it is added to the control directly but when I click on the picturebox nothing happens).
I assume it has something to do with my inheritance. I have a usercontrol called AbstractPage (its not really abstract since the designer doesnt like that) which only consists of a heading and this picturebox but it provides quite some functions the actual pages rely on.
#region Constructor
public AbstractPage()
{
InitializeComponent();
lblHeading.Text = PageName;
picLock.Click += new EventHandler(picLock_Click);
}
#endregion
#region Events
void picLock_Click(object sender, EventArgs e)
{
...do some stuff
}
#endregion
The page implementations just inherit this class and add their controls and behavior. We recently figured out that subclassing UserControl is not performant and we lose some performance there, but its the best way to do it (I dont want to c&p function for 25 pages and maintain them).
My pageA looks like this
public partial class PageA : AbstractPage
{
#region Constructor
public PageA()
{
// I dont call the base explicitely since it is the
// standard constructor and this always calls the base
InitializeComponent();
}
#endregion
public override string PageName
{
get { return "A"; }
}
public override void BindData(BindingSource dataToBind)
{
...
}
Anyway, the picLock_Click is never called and I dont know why?
The pages are all put into a PageControl which consists of a TreeView and a TabContainer where the pages are put once I call addPage(IPage)
public partial class PageControl {
...
protected virtual void AddPages()
{
AddPage(new PageA());
AddPage(new PageD());
AddPage(new PageC());
...
}
protected void AddPage(IPage page)
{
put pagename to treeview and enable selection handling
add page to the tabcontainer
}
Thanks in advance
If I understand your problem correctly, this worked for me out of the box (using VS2k8). My code:
public partial class BaseUserControl : UserControl
{
public BaseUserControl()
{
InitializeComponent(); //event hooked here
}
private void showMsgBox_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
}
public partial class TestUserControl : BaseUserControl
{
public TestUserControl()
{
InitializeComponent();
}
}
I moved the TestUserControl to a form, clicked the button and got the message box as expected. Can you paste some more code, e.g. how do you use your AbstractPage?
I found the problem. We are using the Infragistics WinForms but in that case I used the standard picturebox. I replaced it with the UltraPictureBox and now it works.

Categories