1

I have a security method I would like to be able to sprinkle into other classes throughout my program. It is currently an abstract class but I feel it would be more appropriate as an interface. Can someone help me convert it? I wasn't able to call PrincipalIdentity() the same way I could with it being abstract.

public abstract class SecurityBase {

        public GenericPrincipal PrincipalIdentity() {

            WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

            string authenticationType = windowsIdentity.AuthenticationType;
            string userName = windowsIdentity.Name.Split('\\')[1];;

            GenericIdentity authenticatedGenericIdentity = new GenericIdentity(userName, authenticationType);

            string[] Roles = //generate array;    
            GenericPrincipal pr = new GenericPrincipal(authenticatedGenericIdentity, Roles);

            return pr;
        }
    }
  • 3
    Take a look at the difference between interfaces (msdn.microsoft.com/en-us/library/87d83y5b.aspx) and abstract classes (msdn.microsoft.com/en-us/library/sf985hc5.aspx). That should answer your question. – Thomas Stringer Jul 15 '15 at 22:00
  • 1
    It is abstract because it contains logic to get PrincipalIdentity that is shared between the classes that implement it. Interface however would not have an implementation and you would have to supply the implementation yourself in every class that Implements the interface, is that what you want? is that a good design for your case? More details on how it's used would be helpful for a good answer. – Alexus Jul 15 '15 at 22:28
  • @ThomasStringer yes.. the generic "read the docs" answer is often useful... – markokstate Jul 16 '15 at 12:32
  • @markokstate He's not wrong though. Reading the docs would give you a better idea of when to use abstract classes over interfaces (or vice-versa), and give you direct information about how to implement either of them. – Chris Cirefice Jul 16 '15 at 19:53
4

Is there a reason you need this method in your business classes at all? I can't see any reference to this or to other member fields, meaning it's a disconnected utility method that can happily sit in a UserProvider sort of class, a service that is probably orthogonal to your actual business domain.

If you do want to have it as part of your objects, you can simulate implementation inheritance with interfaces using extension methods:

public interface ISecurityEnabled 
{
     // empty marker interface.
}

public static class SecurityExtensions 
{
    public static GenericPrincipal GetPrincipalIdentity
           (this ISecurityEnabled securityEnabledObject)
    {
         // your code here.
    } 
}

This will allow you to call GetPrincipalIdentity on every object that's marked with the ISecurityEnabled interface. If the method actually needs any properties from that object, add them to the ISecurityEnabled interface to have them available in the extension method.

  • I'm attaching it to my View Models to determine if a user has certain privileges(IsInRole()) to save/view items, I will call PrincipalIdentity() from inside the VM and return bools if they can do action. I still haven't completely wrapped my head around creating my own abstractions and interfaces... sure when I see them I understand them. But I think you may be right, it's probably better off as an outside utility. – markokstate Jul 16 '15 at 12:56
  • Ah, it would have been best if you'd mentioned this specific use case in your question, since binding to ViewModels requires properties, not methods, and certainly not extension methods. – Avner Shahar-Kashtan Jul 16 '15 at 16:17
-1

I agree with Alexus. Below is the interface that would go with your abstract above. You cannot put logic into an interface so this is all you get.

public interface ISecurity{
    GenericPrincipal PrincipalIdentity();
}
  • this reads more like a comment, see How to Answer – gnat Jul 16 '15 at 11:06
  • @gnat while you may not like my answer, it is what was asked for. – Jason H Jul 16 '15 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.