User authentication in SilverLight

I wanted to know how to authenticate users in a SilverLight app using their Windows login info.

01: Set the authentication mode to Windows and <deny users="?"/> in <system.web> within web.config
02: Move the silverlight control to Default.aspx and set that as your start page
03: Add the following Page_Load code

  protected void Page_Load(object sender, EventArgs e)
{
IPrincipal p = HttpContext.Current.User;
if (p == null)
throw new SecurityException("No current user");
if (!(p is WindowsPrincipal))
throw new SecurityException("Not a windows user");
if (!p.Identity.IsAuthenticated)
throw new SecurityException("Not authenticated");
Xaml1.InitParameters =
string.Format("user={0},session={1}", p.Identity.Name, Session.SessionID);
}

04: In app.xaml.cs you can now read the InitParameters using e.InitParameters in the Application_Startup method.

2 thoughts on “User authentication in SilverLight

Leave a Reply

Your email address will not be published. Required fields are marked *