How To Auto-login Using ASP.Net FormsAuthentication

For our demo website, demo.quickpm.net, I need to have a default automatic login. Using the ASP.Net Membership and FormsAuthentication an easy way to do auto-login is the following code

string userEmail = "john.doe@gmail.com";
string password = "thepassword";
if (Membership.ValidateUser(userEmail, password))
{
      FormsAuthentication.SetAuthCookie(userEmail, true);
      Response.Redirect("~/");
}

The above code assumes that “john.doe@gmail.com” is a user and “thepassword” is their password.

Advertisements

Mono Asp.Net MVC and Nginx

I’ve been using Mono ASP.Net for a while with Nginx as the server. I recently started playing around with ASP.Net MVC and discovered that it doesn’t play nice when using Nginx as the server. To make ASP.Net work with Mono and Nginx you need to download the Mono source from here. And then modify the file mcs/class/System.Web.Routing/System.Web.Routing/Route.cs
in particular comment out the following lines in the GetRouteData function

if (pathInfo != String.Empty)
throw new NotImplementedException();

If you comment out the above two lines your MVC website should work just fine when hosting it using Mono and Nginx. I don’t know if there are any negative consequences for commenting out the above two lines but I have not encountered any.

Mono FastCGI automatic subdomain apps

As I outlined in my previous post on using Mono with FastCGI and Nginx, I’m hosting multiple ASP.Net applications with Mono. I already have Nginx configured to automatically direct subdomains of quickpm.net to the fastcgi-mono-server2 program using the fastcgi protocol.

I also have the fastcgi-mono-server2 configured using the following options
fastcgi-mono-server2 /applications=cmd.quickpm.net:/:/usr/local/nginx/html/cmd.quickpm.net,quickpm.net:/:/usr/local/nginx/html/quickpm.net /socket=tcp:127.0.0.1:9000

To quickply explain, the /applications argument is setting the cmd.quickpm.net domain to be served by the directory /usr/local/nginx/html/cmd.quickpm.net, and the other sites are set to their respective directories. The /socket option is telling fastcgi-mono-server2 to listen using the tcp protocol on port 9000 of the local host.

My only problem is that adding new subdomains is not automatic. If I want to add a new subdomain to my website I have to shutdown the mono fastcgi server and restart it with the subdomain added to the /applications argument, I could use a config file instead of the command line option but the point is I have to shut the server down and restart it to add subdomains. I want avoid doing this. What I want is an option like the following /applications=%1.quickpm.net:/:/usr/local/nginx/html/%1.quickpm.net so all subdomains are automatically handled.

The only way to do what I want, I believe, is to patch the mono xsp2 source code.

Configuring Mono and ASP.Net on Nginx

For work I need to setup one domain with multiple subdomains. The basic idea for the website is the same as Fog Bugz, in that it’s a web hosted app. And each customer has their own site e.g. johnny.quickpm.net would be johhny’s site.

For the technology side we use ASP.Net Web forums for the actual software, that is johnny.quickpm.net is running an instance of Asp.net WebForms on Mono.  But for the main site quickpm.net I want to run ASP.Net MVC since I like the MVC model and mono now (mostly) supports ASP.Net MVC in the current daily builds.

For the server software I’m using Nginx and the Mono FastCGI interface.  The important configuration bits are
nginx.conf

#user  nobody;
worker_processes  1;
                       	 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '
    #                  '"$status" $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost *.quickpm.net;
	root /usr/local/nginx/html/$host;
        #charset koi8-r;

        access_log  logs/access.log;
	location / {	
		 root /usr/local/nginx/html/$host/;
	 	 index index.html index.htm index.aspx default.aspx Default.aspx;
		 fastcgi_index Default.aspx;
		 fastcgi_pass 127.0.0.1:9000;
		 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$host$fastcgi_script_name;
	 	 include ../conf/fastcgi_params;
	 }

        #location / {
        #    root   html;
	#    index index.html index.html index.aspx default.aspx Default.aspx;
        #}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

To run the mono fastcgi server use

fastcgi-mono-server2 /applications=cmd.quickpm.net:/:/usr/local/nginx/html/cmd.quickpm.net,quickpm.net:/:/usr/local/nginx/html/quickpm.net /socket=tcp:127.0.0.1:9000

Doing the above results in quickpm.net resolving to /usr/local/nginx/html/quickpm.net and cmd.quickpm.net resolving to /usr/local/nginx/html/cmd.quickpm.net also mono will serve the two directories as two separate ASP.Net Applications.

Sqlite: Unable to open database file

The webapp for my project at work was receiving the error “unable to open database file” from SQLite. The error was intermittent hence it was hard to diagnose. I was using a SQLite database on a webserver running xsp2 and mono (i.e. ASP.Net).  A few times I also received the error “System.IO.File: Too many files open” but this only happened once or twice.

I was running the webapp on openSUSE 11, after some dead ends I finally tried opening a large number of file descriptors from a simple console app. That’s how I discovered that by default openSUSE 11 only lets a process open ~1023 file descriptors. But xsp2 opens a large number of file descriptors when it’s serving web pages in fact this is why SQLite was giving the error “unable to open database file”. SQLite couldn’t allocated a new file descriptor for opening the database file since xsp2 had already used all of the available file descriptors. The fix is to add the following lines to /etc/security/limits.conf

* soft nofile 2048

* hard nofile 4096

To check the user file descriptor limits on unix run the command: ulimit -a

Protected: Measuring Complexity

In this post I want to examine one way of measuring code complexity or other similar processes such as business logic. There are already several algorithms available for measuring code complexity such as Cyclomatic Complexity, the article Programming Tools: Code Complexity Metrics gives an overview of several complexity metrics.

We want to measure code complexity by somehow mapping the problem to one of packing. The idea is to map code to a set of 2d polygons (one way is to have each class be its own polygon) and then measure the complexity of the set of 2d polygons through packing. The complexity metric I use for a set, S, of 2d polygons is the density of the best 2d packing of S.

First the mapping of code to polygons needs to be defined. For an object oriented language each object (by object I mean a Java or C++ class) is mapped to its own polygon, similarly for a functional language each function is mapped to a polygon. For the remainder of this article I’ll assume the language is object oriented.

The mapping from a class to a polygon needs to use some simple measures of complexity.

1. The number of calls to distinct functions outside the class (by distinct I mean two calls to strlen() would count as 1 call to a distinct outside function).
2. The number of functions insides the class (this may need merging with 3).
3. The total number of branches inside the class (by branch I mean an if-then statement, case statement, or any similar construct).

Let P be the polygon to be mapped from an object O. The number of vertices of P equals 1. The average interior angle of P will be equal to the interior angle of a regular polygon with 1 vertices but the standard deviation of the interior angles of P will be equal to 2 + 3 (A linear scaling factor might be needed). By perturbering the interior angle of P a more complex shape is created. The element of randomness does mean that the mapping is non-deterministic but that’s not a large problem because measuring code complexity is fundamentally a fuzzy task.

My next post will finally describe a slightly different algorithm for solving tangram puzzles.