Friday, September 28, 2012

Javascript structural pattern

1. Prototype Pattern,

<script type="text/javascript">
    var Calculator = function (id) {
        this.id = id;
    };
    Calculator.prototype = {
        add: function (x, y) {
            var result = x + y;
            $(this.id).html("<br>" + result + "</br>");
        },
       
        multiply: function (x, y) {
            var result = x * y;
            $(this.id).html("<br>" + result + "</br>");
        }
    };

    $(document).ready(function () {
        var c = new Calculator('#a');
        c.add(12, 32);
        c.id = '#b';
        c.multiply(2, 10);
       
    });
</script>

<div id="a"></div>
<div id="b"></div>

Also arguments can be used for variable length of parameters within function to be able to do things like test(a), test(a, b), test(a, b, c) etc...

2. Module Pattern - to hide the private property/method

var Calculator = function(id) {
    //private members
    var id = id;
    //public members
    return {
         add: function (x, y) {
               $(id).html( x+y);
         }
    };
};

Client:
var c = new Calculator('#a');
c.add(3, 5);

3. Revealing Module Pattern
var calculator = function(id) {
       var id = id;
       var doAdd = function (x, y) { $(id).html (x + y);
       return {
             add  : doAdd;
       }
         
}('#a'); //use this to self invoking.

Client:
calculator.add(3, 5);

4. Revealing Prototype Pattern
var Calculator = function (eq) {
    //this. variables.
};

Calculator.prototype = function() {
     // private members
     var doAdd = function (x, y) {return x + y;}
     return {
           add : doAdd;
     };
}();


.Net 4.5 Security Claims

PluralSight has a quite detailed course to explain the changes in Indentity and Access Control in .Net 4.5, by Dominick Baier.

1. In this video, it shows the new change in .net framework security. The most important change is now the GenericIdentity, WindowsIdentity has inherited from the base class as ClaimsIdentity. All the claims can be accessed from the Claims property. The same is happened in Principle as well, which is ClaimsPrincipal.


 
 

2. How do you extend the claims authentication and authorization in .Net 4.5?

ClaimsAuthenticationManager is a base class to extend to transform the incoming ClaimsPrincipal.
It has a override method,
ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)

By updating this method, the incomingPrincipal can be transformed to add/update/remove some extra claims. This is especially useful in federated security scenario. When the identity party authenticated the responsiblie party and send the incoming principal. The server can transform it to provide some extra or their own meaning for the claims.

ClaimsAuthorizationManager is a base class to extend claims based authorization.
It provides the method
bool CheckAccess(AuthorizationContext context)

The claims authorization is different from the previou role-based authorization. The role based authorization is a roles string array, to ask if the user is in a role. Claims based authorization has the concept as Action and Resource to ask if the action for the rsource is permitted or not.

So in the AuthorizationContext, it has theree properties. Action, Resource and Principal.
ClaimsPrincipalPermission is an attribute to decorate on the method for authorization.

To use the custom authentication and authorization classes, the classes need to be include in the config file such as,



For the authentication transfor, using the code,

Thread.CurrentPrincipal = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager.Authenticate("none", incomingPrincipal);

to transform the incoming principal to custom format.

Also for the project, System.IdentityModel, System.IdentityModel.Services are the necessary project references.

Wednesday, September 26, 2012

WCF Version Strategy

Just went through the following code project articles, which detailed explains the WCF version in all possible scenarios.

WCF Backwards compatible & version strategy: Part 1, Part 2, Part 3

In summary, WCF works best try to match the data contract from the client side to the service implementation. They will throw errors for the "Required" data member when they are not presented.
Also there is a concept called "Version round trip" so the new data member can be preserved and then sent back. But the data contract has been inherited from IExtensibleDataObject

For the version strategy, it categories as strict, semi-strict and agile versioning.

The difference between strict and semi-strict is whether the service contract interface_version2 inherit the interface_version or not. If v2 inherit from v1, then it is semi-strict, and if v2 just include the methods from v1, that is strict versioning. Also the basic idea is the service contract implementation should always implement the interface v1 and v2.

Also there is a blog talking about the general WCF version guidelines

Wednesday, September 12, 2012

SingleWsdl in framework 4.5



Check the diagram above, after I built the service in .net 4.5 and browsing the svc, now there is a new option to return all schemas in one single wsdl, which I blogged before.

This is exetremely useful for BizTalk published service scenario. BizTalk solution usually defines many schemas for each operation in the service, and it is quite hard to distribute the wsdl files to another team to use it. Now it is much easier!

ServiceBus relay service with hosting in IIS

Nowadays in most of the cases, custom developed wcf services are hosted in IIS with windows appfabric, which still based on WAS. So if the services are blocked by firewall and needed to be used by 3rd party, ServiceBus relay service will be a good solution for it.

However, for the services always have to open an outbound port and create a bidirectional socket for communication, which connect to the Service Bus, authentication and listening messages before the client sends request. And for IIS hosting, they only start service when the first request comes through.
This is actually a difficulty, and well explained here.

I just followed the tutorial here and used windows appfabric to enabled the auto-start feature, then it worked.

Thursday, September 6, 2012

Update: signalR-0.5.3 and mvc4 worked

I want to try out the signalR library in mvc 4, so pretty much I follow the basic tutorial to have the basic "chat" program. First,  I run Install-Package "signalR" to install the packages in to the project.
After quickly get the codes there, and hit F5. Oops, it doesn't work. Then here is long hours google and try but I just have no luck. Browsing to "/signalr/hubs" just always gives me the 404 error.

Just an experiment, I recreated a mvc 3 project and running the same codes. This time, browse to "/signalr/hubs" actually returns me the proxy javascript. But I still have javascript error when I tried to send the message (strat to chat). However, If I use an old version (0.3.4), it all WORKS prefectly. The version I get from the msdn sample in here.

Update: Just saw on 11 Sep, there is a new update from Nuget. So I installed it to my MVC4 project.
But specifically in the package manage console, I am running,
install-package signalR -version 0.5.3 <projectname>

Then it all worked. only still in the _layout file,
I am using
<script src="@url.Content("~/scripts/jquery.signalr-0.5.3.min.js") type="text/javascript"></script>
rather than
mvc4 bundle javascript, @Scripts.Render(...)

Next, we can explore how to use signalR within BizTalk to be able to update the client the service/task progress.




Tuesday, September 4, 2012

Jquery file upload in ajax

Recently need to upload pictures to server side, and it should be in ajax way with a progress bar.
The default $.ajax doesn't support form data posted. So here is the jQuery Form Plugin to rescue.
And there is a blog example to use it within asp.net mvc.
Update: just find another blog with links to 7 javascript ajax upload library