RSS

Tag Archives: c#

JavaScript Bundle Doesn’t Work on ASP.Net MVC 5

Developers often make this very common mistake. You start an ASP.Net MVC app, add few JavaScript files and libraries, run. Everything works fine but the JavaScript.

ASP.Net MVC Bundling

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-1.*"));

    bundles.Add(new ScriptBundle("~/bundles/startstop").Include(
                "~/Scripts/startstop*"));
}

ASP.Net MVC bundle has default ignore list and one of ignore list is ‘*.min.js’ files. Yes, any minified JavaScript file.

You can add the following code RegisterBundles to clear the ignore list before adding anything to the bundle.

bundles.IgnoreList.Clear();

Once you clear the list, you are also able to build your own ignore list.

bundles.IgnoreList.Ignore("");
 
Leave a comment

Posted by on June 12, 2015 in General

 

Tags: , ,

Bigger Rock, But Still!!!

OK, after the smaller rock, I got a slightly bigger rock now… but I still think it’s a stupid mistake. I hope you guys don’t make the same one.

Instead of a internal setter, I removed the setter at all. Well, it didn’t serialize with my data at all! Everything else came through correctly but the properties without setter.

[DataContract]
public class Employee
{
    [DataMember]
    public int ID { get; }
    [DataMember]
    public string Firstname { get; set; }
}

What basically happens here is WCF creates XML schema for classes it needs to return as a response. The XML schema it creates is similar to C# class, but without C#-specific syntax like access modifier. The purpose of this is so that WCF can serialize the data and serve it as SOAP response, which is XML-based. The creation of the schema, ostensibly, ignores properties without setter.

Slightly bigger rock, but I still got tripped. Oh well..

 
Leave a comment

Posted by on June 10, 2015 in General

 

Tags: , ,

Boxing x Unboxing

Boxing and unboxing is not to be confused with downcast / upcast.

  • Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.
  • When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
  • Unboxing extracts the value type from the object.
  • Boxing is implicit
  • Unboxing is explicit.
  • The concept of boxing and unboxing underlies the C# view that a value of any type can be treated as an object.

Example:

Boxing

int i = 123;
// The following line boxes i. 
object o = i;

Unboxing

o = 123;
i = (int)o;  // unboxing

Reference: MSDN

 
Leave a comment

Posted by on June 5, 2015 in General

 

Tags: , , , ,

It’s The Small Rock, Agggh!!

I have this stupid mistake that I hope none of you would ever make.

I am building a WCF service and one of the DTOs is something like this:

[DataContract]
public class Employee
{
    [DataMember]
    public int ID { get; internal set; }
    [DataMember]
    public string Firstname { get; set; }
}

On the ‘other’ side (client), I can’t set my Employee’s ID… Hours of debugging turned to “dooohhhh” moment when I realized the setter was in fact internal. Internal is internal, access modifier that can’t be accessed from outside library. Easily miss the basic when you deal with higher level stuff.

It’s the small, tiny rock that trips you. Aggh!

 
Leave a comment

Posted by on June 4, 2015 in General

 

Tags: ,

Claims-Based Authorization in ASP.Net MVC and Web Api

Normally, I would re-write a blog post in hope to provide better explanation and concise the article, but since this is from Dominick Baier, I think he does it the best.

So, here’s it, how to apply claims based authorization in ASP.Net MVC and Web Api:

http://leastprivilege.com/2012/10/26/using-claims-based-authorization-in-mvc-and-web-api/

 
Leave a comment

Posted by on May 28, 2015 in General

 

Tags: , , , , ,

Unit Testing WIF’s ClaimsPrincipalPermission.CheckAccess

WIF 4.5 has ClaimsPrincipalPermission.CheckAccess method, very useful to check user’s authorization. You can use this as method call or attribute.

// Imperative method call
using System.IdentityModel.Services;
public ActionResult Index()
{
    ClaimsPrincipalPermission.CheckAccess("foo", "bar");

    return View();
}

// Attribute
[ClaimsPrincipalPermission(SecurityAction.Demand, Operation="foo", Resource="bar")]
public ActionResult ViewFoobar()
{
    return View();
}

Either way, how do we unit test this? My approach is to first abstract out ClaimsPrincipalPermission and create a new wrapper class that will be injected to the dependent class.

Abstract Out

using System.IdentityModel.Services;

public class ClaimsPrincipalWrapper : IClaimsPrincipalWrapper
{
    public void CheckAccess(string resource, string action)
    {
        ClaimsPrincipalPermission.CheckAccess(resource, action);
    }
}

Dependency Injection

using System.IdentityModel.Services;

public class HomeController : Controller
{
    private readonly IClaimsPrincipalWrapper _ClaimsPrincipalWrapper;

    public HomeController(IClaimsPrincipalWrapper claimsPrincipalWrapper)
    {
        _ClaimsPrincipalWrapper = claimsPrincipalWrapper;
    }

    public ActionResult Index()
    {
        _ClaimsPrincipalWrapper.CheckAccess("foo", "bar");

        return View();
    }
}

Unit Test

[TestMethod]
public void TestIndex()
{
    // Arrange
    var _claimsPrincipal = new Mock<IClaimsPrincipalWrapper>();
    _claimsPrincipal.Setup(m => m.CheckAccess(It.IsAny<string>, It.IsAny<string>));
    var _controller = new HomeController(_claimsPrincipalMock.Object);

    // Act
    var _result = _controller.Index() as ViewResult;

    // Assert
    Assert.IsTrue(_result.View != null);
}
 
Leave a comment

Posted by on May 4, 2015 in General

 

Tags: , , , , , , ,

Upcasting x Downcasting

Upcasting

Is when you cast an object to its base class type.

public class Person
{ }

public class President : Person
{ }

void Main()
{
    var president = new President();

    // Upcasting
    var person = (Person)president;
}

Downcasting

Well, it’s the opposite of upcasting, when you cast a reference of a base class to one of its derived classes.

You can only downcast when the object’s type is original a derived class, so it’s important to perform some checking prior, such as using as or is operators.

public class Person
{ }

public class President : Person
{ }

void Main()
{
    var president = new President();
    var person = (Person)president;

    // Downcasting
    var president = (President)person;
}
 
1 Comment

Posted by on April 27, 2015 in General

 

Tags: , , ,

 
%d bloggers like this: