5

I am playing around with FluentSecurity library for asp.net mvc. One of the interfaces exposed by this library is ISecurityContext as shown below:

public interface ISecurityContext
{
    dynamic Data { get; }
    bool CurrenUserAuthenticated();
    IEnumerable<object> CurrenUserRoles();
}

When I try to access "Data" property (as shown below) it is not available. Although the other two methods seems to be accessible.

public class ExperimentalPolicy : ISecurityPolicy
{
    public PolicyResult Enforce(ISecurityContext context)
    {
        dynamic data = context.Data; // Data property is not accessible.
    }
}

What am I missing ? Thanks.

  • What is the actual exception, I would think this issue would be with the actual implementation of ISecurityContext due to what is being returned by Data rather than calling data itself, because whether or not the return type is dynamic, Data itself is a static property. – jbtule Aug 15 '12 at 13:56
  • @jbtule see my comment to Hogan's answer – user1599610 Aug 15 '12 at 18:04
  • 1
    Your comment doesn't have the Exception in it. But it does mention working when you use the source but not the dll. Is this a compiler error instead? Are you not using the 1.4 dll's from nuget because before 2.0 there wasn't a Data property on the ISecurityContext https://github.com/kristofferahl/FluentSecurity/blob/a7b48011448ec366c664b83a4fb0fb0db44434d4/FluentSecurity/ISecurityContext.cs – jbtule Aug 15 '12 at 18:24
  • @jbtule thank you. I was using 1.4 reference while the source code i downloaded was 2.0. I feel silly :) Anyways..if you can put down your comment as an answer I can mark it as one. – user1599610 Aug 15 '12 at 19:14

2 Answers2

2

The Data property on ISecurityContext isn't introduced until version 2.0. The default installed with nuget without including prerelease is 1.4. Which does not have the property. Make sure you are using the right version!

jbtule
  • 31,383
  • 12
  • 95
  • 128
0

The following ran as expected, is there anything I'm doing differently than you are?

void Main()
{
  ATest t = new ATest();
  Experiment z = new Experiment();

  z.TestTest(t);
}

public class ATest : ITest
{
  public dynamic Data {get; set;}

  public ATest()
  {
     Data = new { Test = "This is a string" };
  }
}

// Define other methods and classes here
public interface ITest
{
  dynamic Data { get; }
}

public class Experiment
{
    public int TestTest(ITest context)
    {
       dynamic data = context.Data; 

       Console.WriteLine(data.Test);

       return 0;
    }
}
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • @Abhinav - yes you are right, I did some tests and posted something not based on guesses (so your comment might seem strange now) – Hogan Aug 15 '12 at 03:02
  • @Hogan Your code works fine but the problem is another one: the interface is given by an external library. – Omar Aug 15 '12 at 03:06
  • @Fuex, I've no idea why that would matter. – Hogan Aug 15 '12 at 03:07
  • @Hogan Firstly I thought it like you, but reading the msdn's docs it seems that this would matter. – Omar Aug 15 '12 at 03:11
  • @Hogan Surprisingly this does matter. I am confused as to why. If i write the same class in FluentSecurity's source code it works. But the same thing does not work when referencing FluentSecurity.dll in asp.net mvc project. I am confused why this restriction. – user1599610 Aug 15 '12 at 05:17
  • @Hogan you shouldn't return anonymous types using dynamic. I know this is test code, but because the anonymous type is marked internal, it's inaccessible when crossing assembly boundaries. It's possibly that this is the same issue that user1599610, but it's to hard to tell from the context. – jbtule Aug 15 '12 at 14:00