summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Web/AuthenticationHttpModule.cs
blob: c90c317dec2e5d543b4af33058581d4ac2e7d2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System.Security.Principal;
using System.Web;
using System.Web.Security;

namespace Marina.Web {
	public class AuthenticationHttpModule : IHttpModule {
		public void Init( HttpApplication context ) {
			context.AuthenticateRequest += delegate { AuthenticateHttpRequest( ); };
		}

		public void Dispose() {}

		public void AuthenticateHttpRequest() {
			HttpCookie cookie = GetCookieFrom( HttpContext.Current );
			if ( null != cookie ) {
				BindPrincipalToThreadUsing( FormsAuthentication.Decrypt( cookie.Value ).Name );
			}
		}

		private HttpCookie GetCookieFrom( HttpContext context ) {
			return context.Request.Cookies[ FormsAuthentication.FormsCookieName ];
		}

		private void BindPrincipalToThreadUsing( string username ) {
			HttpContext.Current.User = new GenericPrincipal( new GenericIdentity( username ), new string[] {"Customer"} );
		}
	}
}