Apache Cordova and Visual Studio 2015 – To Do sample application

I want to explore Apache Cordova and the new tools available at Visual Studio 2015.

There is a sample application walkthrough we can use to practice Cordova with VS 2015, I selected the AngularJS version of the sample. You can also find the source code for the sample here Source Code Link.

The application keeps record of to do activities users enter. It stores the tasks in a local Html5 storage and, if you have access, in a Azure SQL server database using an Azure Mobile Service.

Had no issues following the walkthrough up to making it work with the local storage. The problem was when I tried to use Azure storage through the mobile service: the application simply didn’t  show any data neither saved it to the database.

It result that I had to configure the mobile service and add the table from it, so, I created another mobile service from Azure console, and followed the steps at the link Configure Mobile Service. Then, I went back to the code and set the Mobile Service API Key and the Url of the service at the service file azureStorage.js.

 

Running sample on Android VS 2015 emulator

Running sample on Android VS 2015 emulator

Cordova allows access to functionalities like geo location, camera, etc. while using JavaScript and AngularJS, TypeScript and other known web development frameworks.

I think there are good opportunities for taking advantage of all of these technologies so to quickly develop apps and avoid difficult to maintain mobile applications, developed in native to the device code.

I plan to keep improving on web development frameworks, specially JS, AngularJS and TypeScript. I’m sure we will find a good business use of them at my job at Zgura!

Cheers,

Danfer.

Learning AngularJS and use it with ASP.Net MVC – Controller on MVC view

I’ve been using book Build Single Page Application with ASP.Net MVC 5 and AngularJS.

On chapter 4, at Creating first angular controller, book’s code didn’t work in my case. I’m using Angular version 1.3.15.

I had to make following changes:

At _Layout.cshtml, identify the app:

<html ng-app=”myApp”>

At homeIndex.js (the file storing the angular controller definition):

//homeIndex.js

var app = angular.module(myApp, []);

app.controller(‘homeIndexController’, function ($scope) {

$scope.fname = “John”;

$scope.lname = “Doe”;

});

 

And at the view, from where the angular controller is supposed to be called, Index.cshtml:

@section scripts

{

<script src=”~/js/homeIndex.js”></script>

}

<div ng-app=”myApp” ng-controller=”homeIndexController”>

Fname:- {{fname}} <br />

Lname:- {{lname}}

</div>

Will write this comment at Amazon too. Either AngularJS has changed since the version author was using (he says it is 1.2.26 or there may be some errors on sample code at the book.

Cheers.

Custom attribute with WIF claims based authorization

WIF is Microsoft framework for building applications using token security services and claims. Claims are like attributes that describe the user. An example could be the claim Role with a value “Administrator”. Another example, claim Location, value “ManaguaOffice1”.

I can develop an application that can validate claims and act in consequence, like allowing access to some functionality based on Role or location claims.

You can find a nice explanation on the topic here: Introduction to claims based authentication in .Net

I’d like to share an implementation of claims based authorization. This is a solution we developed at a ASP.Net MVC 5 project that uses WIF and OWIN security. We needed to provide a simple way to set authorization for users to access application functionality based on their roles.

As the functionality users have access to is implemented using ASP.Net MVC controllers and controller’s action methods, we needed a custom attribute that could be applied to either controller or action level. We developed an alternative custom attribute, here the code:

//
//Enables custom claims based authorization.
//It uses role claim type.
//Role values get from Auth DB.
//

public class RoleAuthorizeAttribute : AuthorizeAttribute
{

private string[] claimValues;

public RoleAuthorizeAttribute(string allowedRoles)
{
claimValues = allowedRoles.Split(‘,’);

var userProfile = UserProfileRepository.Get(HttpContext.Current.User.Identity);
var identity = UserProfileRepository.AddClaimsRole(userProfile, HttpContext.Current.User.Identity);

}

public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{

bool isInRole = false;

foreach (var str in claimValues)
{
//It can manages white spaces before and after the role name.

isInRole=HttpContext.Current.User.IsInRole(str.Trim());

if (isInRole) break; //break foreach as user is in one of the required roles.
}

if (isInRole)
{
base.OnAuthorization(filterContext);
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}

}
}

This class inherits from AuthorizaAttribute class and overrides the onAuthorization action filter to allow to “inject” the custom attribute. It can work for more than one role. I’d implement it at the controller like below:

[HttpPost]
[RoleAuthorize("user, admin")]
public ActionResult MyAction(int id)
{
....

 

Cheers.

What I’m currently working on

First of all, doing really cool things. Just to mention a few:

Keep working on telecommunication services and ecommerce platform at Zgura.

We are integrating services from multiple vendors adding our services. We are using MVC 5 and Web API 2.

We are also using Microsoft Azure websites, SQL databases. We use different websites to support user interfaces and services. We are using Azure Scheduler and we have several jobs running, triggering process through our Web API applications.

Another interesting aspect of the job is building processes that involve some business intelligence so to allow the platform to take business decisions automatically. Last is work in progress.

Cheers,

Danfer.

Starting the year – Improvement through creativity

First post for 2014. I made my New Year’s resolutions, professional field, creating great software with Zgura:

  1. Learn, get certified for Web development, exams 70-480, 70-486 and 70-487 link
  2. Apply SOLID development principles
  3. Always try to improve through creativity

On 3, I’d like to comment on the ideas expressed at Fareed Zakaria GPS program, in an interview to Elon Musk GPS link, summarizing, people working at his company must not just develop technology, they must improve on what has been developed. Just doing one work is not enough, need to improve on existent work.

As such, I’d like to share on an improvement I just implemented, developing on work already done by my colleagues at Zgura:

As we develop ASP.Net MVC apps and deploy continuously from Git into Azure Websites, one website for each stage of development and branch on source control (i.e., development, test, production) we wanted to set and use configuration environment variables for between other things, connection strings to databases, in Azure Websites and get them from Environment variables, using:

public static string ConnectionString
        {
            get
            {
                return Environment.GetEnvironmentVariable("APPSETTING_ConnectionString,"varname");
            }
        }

But after implementing that, we had an issue when trying to do local debugging as the application doesn’t have access to the environment variables repository. A solution I found was to add that variables at Application_Start, under Global.asax.cs:

#if DEBUG
            Environment.SetEnvironmentVariable("APPSETTING_ConnectionString","varname");

#endif

Cheers.

P.d.

As per a comment from my friend Salvador Aguilar, I’d like to show a screenshot of deployment configuration into Azure websites.

Continuous Deployment Azure

MVC 4, Keeping Convention over Configuration

I’ve found myself adding several actions methods to a controller, repeating the action method type, like for example, having more than one Index method at the controller. Of course, I add something like ProcessAIndex, ProcessBIndex, etc.

I believe that may not be according to MVC principle of convention over configuration, so think I should have more controllers. I also think I can use Areas to mitigate controllers proliferation I’ll get. Will investigate more and will keep updated.

Una semana súper ocupada (como todas)

Pero ha sido una semana de muchos éxitos trabajando con C# y MVC 4 en la plataforma de trazabilidad de proteina para Zgura.

Hemos implementado localización, membresía y autorización. Por cierto esos son temas de nuevos posts para este blog.

Una recomendación, si alguién empieza a trabajar con MVC 4, en mi humilde opinion (IMHO) es mejor comenzar de una vez utilizando las herramientas de la plataforma (MVC 4): código primero (pertenece en realidad a Entity Framework, Code First), modelos de vistas en vez de viewbag, fuertemente tipado en las vistas (views), etc.

Utilizar las herramientas que nos da esta tecnología nos ahorra mucho trabajo, cuando la aplicación crece y se le tiene que dar mantenimiento, lo anterior es de gran ayuda.

MVC 4 investigating best practices

I’m starting to investigate on the topic. We are looking for improvements on MVCdevelopment.

One point have noticed: improve usability of UI. How to avoid having to jump to other views to realize actions over current object instances?

I think we can help on this problem using partial views and or Ajax. Will keep looking for these alternatives more deeply.