house9

random code and what not

Fixing asp.net MVC AuthorizeAttribute

With very little code we can fix asp.net MVC AuthorizeAttribute
What problems does it have? Too much redirection.

  • When making ajax calls a HTTP 401 (Unauthorized) would be better than a redirection
  • If I am already logged in but access a secure resource (controller / action) redirecting to the login page is far from ideal, an access denied view makes more sense
When is the built in redirection appropriate? When making standard HTTP request and the user is not authenticated.

it turns out it is relatively easy to fix these issues
  • inherit AuthorizeAttribute
  • override HandleUnauthorizedRequest
#1: Ajax request should not return redirection / html response

if the user cannot authorize an action and the request is made via ajax we don’t want 200 or 302 response codes

we do want 401 Unauthorized, but we have to settle for a 403 Forbidden

The code to fix this

#2: Authenticated users should not redirect to the login page, they should get an Access Denied page
The code to fix this

Turns out very little code is needed - but seems like some of this should just be built in? Using the 401 response won’t work because the asp mvc framework must be picking that up later on and forcing the redirection to the login page, the 403 is not ideal but it is effective.

The code for our class - AuthorizeFor


Usage of our AuthorizeFor attribute


Resources

Log: Wildlife sightings summer 2010

Came across some notes from my vacation last summer, figured I might as well post them on the net (better late then never). Not in any order and contains both spelling and grammer errors, enjoy:


2010-06-27 UT,AZ,ID:
Swainsons Hawk
Black Throated Sparrow

2010-06-30 Zion
Rocky Mountain Bighorn Sheep

2010-07-01 Uinta, UT:
Tanager
Kestrel
Robins

2010-07-01 Near Yellowstone:
Bald Eagle
Osprey

2010-07-01 - 2010-07-04 Yellowstone:
Grizzley Bear + 2 cubs
Girizley Bear + 3 cubs
Red Tail
Gray Jay
Mtn Blue Bird
Mule Deer
Cyotes
Otters
Junco
Common Muskrat
Deer Mouse
Chipping Sparrow
Cassins Finch
Cliff Swallow
Brewers Blackbird
Cowbird

2010-07-04 Leaving Yellowstone -> Cody, WY:
Black Bears
Sandhill Cranes
2 Coopers Hawks
2 Golden Eagles
Harrier (Male)
Waxwings
Terns

2010-07-06 Big Horn National Forest, WY:
Moose
Marmots

we went on to the Black Hills of South Dakota, Montana, and Alberta after this, but I must have stopped keeping track of sightings, there were plenty more…

Some notes - Ubuntu install rails 3 environment

Had this on my desktop, notes to set up Ubuntu install rails 3 environment

Ubuntu install rails 3 environment
================================================

1) Get RVM installed with ruby 1.9.2
- http://www.christopherirish.com/2010/08/25/how-to-install-rvm-on-ubuntu-10-04/

2) Install Rails 3 gem
- gem install rails --no-rdoc --no-ri
- .... Successfully installed rails-3.0.5 ... 23 gems installed

3) Install redcar 'ruby ide'
- check out
- http://azazeal.xelixis.net/post/How-to-install-Redcar-editor-under-Ubuntu-1010-with-RVM.aspx
- for my situation I am only installing it on ruby 1.9.2, if you plan to run multiple versions of ruby this probably will not work when you have your rvm switched to another version
- gem install redcar --no-rdoc --no-ri
- redcar install
- now you should be able to type redcar at the terminal to launch the gui
- lets make a desktop shortcut
- right click on the desktop : Create Launcher
- Type: Application
- Name: Redcar
- Command: /home/YOUR_USER_NAME/.rvm/gems/ruby-1.9.2-head/gems/redcar-0.11/bin/redcar
- Comment: Ruby IDE
- need a tight icon for our launcher
- right click > Properties > click the emblem icon
- Path: /home/YOUR_USER_NAME/.rvm/gems/ruby-1.9.2-head/gems/redcar-0.11/share/icons/redcar-icon-beta.png
- drag the desktop icon up to the launcher bar if you want one there as well
- need to set the icon path again for that one

4) Create a rails application
- mkdir projects; cd projects
- rails new sample_app_01; cd sample_app_01
- in redcar > File > Open Directory > projects/sample_app_01
- ctrl+T > type Gem > open Gemfile
- verify line : gem 'sqlite3'
- back to the terminal
- bundle
- rails g scaffold post title:string body:text
- rake db:migrate
- rails s
- in a browser go to http://localhost:3000/posts


Sorry the links above are not hot, just cut and paste from my notes into a pre tag - quick and dirty

Notes - TeamCity setup on Ubuntu

Notes from setting up TeamCity on Ubuntu

TeamCity on Ubuntu
==================================================================
Go to http://www.jetbrains.com/teamcity/download/ and download the linux distribution

set JAVA_HOME : update .bashrc add the following lines
--------------------------
export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk/bin/java
export PATH=$PATH:/usr/lib/jvm/java-1.6.0-openjdk/bin

tar xfz TeamCity-6.0.3.tar.gz
put extracted TeamCity directory under ~/TeamCity

open terminal and run bin/runAll.sh start from the ~/TeamCity directory

install tcWebHook plugin
==================================================================
http://tcplugins.sourceforge.net/files/tcWebHooks
- http://sourceforge.net/apps/trac/tcplugins/wiki/TcWebHooks

create directory at ~/.BuildServer/plugins - tcWebHooks
put the jar file in that directory
restart the teamcity server
there should be a new tab 'web hooks' on the build results, not in the admin section

Could not find generator delayed_job_migration.

Maybe this post will help somebody? just installed delayed_job on a rails 3 application. The readme for delayed_job is for the older version, here is a link to the newer docs.

Running this command
rails g delayed_job_migration
Returned this error
Could not find generator delayed_job_migration.
With rails 3 the command should be
rails g delayed_job

Comments

Zac Kloepping
Helped me! Thanks!

Ping Guest OS from Host OS with VirtualBox

I am running a host Windows 7 machine with a Guest Windows Server 2008 RC2. The guest machine is hosted inside of VirtualBox. It appears that this issue can occur with any number of OS host or guest machines.

From the guest machine I could access the internet and access the host machine, but I could not ping or access the guest machine from the host. Turns out there is a network setting that needs to be changed.

On the guests ‘container’ go to Devices > Network Adapters


Then change ‘Attached to’ from ‘NAT’ to ‘Bridged Adapter’


at that point you should be good to go

Resources

Comments

Diego Rafael Flores Gonzales
I did that, they are in the same network but I can't ping from either side, What do I have to do? the other comments says to change the setting from public to private HOW?
Darren Helsby
Thanks for your post. I was having the same problem between a Win 7 Pro host and a Win 8 Customer Preview guest and solved it using your method.

The only extra thing I had to do was change the guest network setting from a public network to private and now I can ping both ways.

Rows and Columns with CSS

here is a code snippet for creating rows and columns with css - I guess you could say this is a table?




Resources

Comments

W.RAZI.EU
Hi, is it possible to have the left column width fixed, 300px for example and right to stretch across the page?

Add tf.exe and other .net command line tools to your powershell path

I really don’t like cmd.exe, it is difficult to work with. PowerShell ISE is much nicer and the bonus is you can run non powershell programs from this environment as well.

I needed to run some TFS commands, adding the ide directory to the path is easy
$env:path += “;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”
another directory you might want to add if you are messing about with IIS7

$env:path += “;C:\Windows\System32\inetsrv” 
now you can do things like recycle all of the AppPools on your machine

appcmd list apppool /xml | appcmd recycle apppool /in

asp mvc jquery from cdn with fallback to localhost

Paul Irish has some nice code for loading jquery from cdn but then falling back to localhost if it cannot be loaded from cdn. Check it out here.

For asp mvc you usually also want jquery.valdiater and possibly unobtrusive validation as well. Here is an html helper to generate html mark up that handles loading these via cdn with fallback

The helper



Usage, most likely from your layout file



And the rendered html

Powershell script to generate a TODO report

You can use this from your CI build process, help keep the code base clean. I am a big fan of the TODO: comment, but you don’t want too many building up in the code base at some point you either need ‘TO DO IT’ or else forget about it.




the above script is excluding all files found under lib or Test directories, and selecting only a subset of file types. Modify to your needs