using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security;
using System.Security.Principal;
using System.Web.Security.SingleSignOn;
using System.Web.Security.SingleSignOn.Authorization;
public partial class _Default : System.Web.UI.Page
{
const string NullValue = "<span class=\"abbrev\" title=\"Null Reference, or not applicable\"><b>null</b></span>";
static Dictionary<string, string> s_abbreviationMap;
static _Default()
{
s_abbreviationMap = new Dictionary<string, string>();
//
// Add any abbreviations here. Make sure that prefixes of
// replacements occur *after* the longer replacement key.
//
s_abbreviationMap.Add("System.Web.Security.SingleSignOn.Authorization", "SSO.Auth");
s_abbreviationMap.Add("System.Web.Security.SingleSignOn", "SSO");
s_abbreviationMap.Add("System", "S");
}
protected void Page_Load(object sender, EventArgs e)
{
//
// Get the user's identity from their ADFS token.
//
SingleSignOnIdentity ssoId = User.Identity as SingleSignOnIdentity;
//
// Get some property tables initialized.
//
PagePropertyLoad();
IdentityLoad();
BaseIdentityLoad();
SSOIdentityLoad(ssoId);
SecurityPropertyTableLoad(ssoId);
//
// Filling in the roles table
// requires a peek at the viewstate
// since we have a text box driving this.
//
if (!IsPostBack)
{
UpdateRolesTable(new string[] { });
}
else
{
GoGetRoles(null, null);
}
//
// Get the right links for SSO
//
if (ssoId == null)
{
SignOutUrl.Text = "Single Sign On isn't installed...";
SignOutUrl.Enabled = false;
}
else
{
if (ssoId.IsAuthenticated == false)
{
SignOutUrl.Text = "Sign In (you aren't authenticated)";
SignOutUrl.NavigateUrl = ssoId.SignInUrl;
}
else
SignOutUrl.NavigateUrl = ssoId.SignOutUrl;
}
}
void SecurityPropertyTableLoad(SingleSignOnIdentity ssoId)
{
Table t = SecurityPropertyTable;
if (ssoId == null)
{
AddNullValueRow(t);
return;
}
//
// Go through each of the security properties provided.
//
bool alternating = false;
foreach (SecurityProperty securityProperty in ssoId.SecurityPropertyCollection)
{
t.Rows.Add(CreateRow(securityProperty.Uri, securityProperty.Name, securityProperty.Value, alternating));
alternating = !alternating;
}
}
void UpdateRolesTable(string[] roles)
{
Table t = RolesTable;
t.Rows.Clear();
bool alternating = false;
foreach (string s in roles)
{
string role = s.Trim();
t.Rows.Add(CreatePropertyRow(role, User.IsInRole(role), alternating));
alternating = !alternating;
}
}
void IdentityLoad()
{
Table propertyTable = IdentityTable;
if (User.Identity == null)
{
AddNullValueRow(propertyTable);
}
else
{
propertyTable.Rows.Add(CreatePropertyRow("Type name", User.Identity.GetType().FullName));
}
}
void SSOIdentityLoad(SingleSignOnIdentity ssoId)
{
Table propertyTable = SSOIdentityTable;
if (ssoId != null)
{
PropertyInfo[] props = ssoId.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
AddPropertyRows(propertyTable, ssoId, props);
}
else
{
AddNullValueRow(propertyTable);
}
}
void PagePropertyLoad()
{
Table propertyTable = PageTable;
string leftSidePath = Request.Url.GetLeftPart(UriPartial.Path);
propertyTable.Rows.Add(CreatePropertyRow("Simplified Path", leftSidePath));
}
void BaseIdentityLoad()
{
Table propertyTable = BaseIdentityTable;
IIdentity identity = User.Identity;
if (identity != null)
{
PropertyInfo[] props = typeof(IIdentity).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
AddPropertyRows(propertyTable, identity, props);
}
else
{
AddNullValueRow(propertyTable);
}
}
void AddNullValueRow(Table table)
{
TableCell cell = new TableCell();
cell.Text = NullValue;
TableRow row = new TableRow();
row.CssClass = "s";
row.Cells.Add(cell);
table.Rows.Clear();
table.Rows.Add(row);
}
void AddPropertyRows(Table propertyTable, object obj, PropertyInfo[] props)
{
bool alternating = false;
foreach (PropertyInfo p in props)
{
string name = p.Name;
object val = p.GetValue(obj, null);
propertyTable.Rows.Add(CreatePropertyRow(name, val, alternating));
alternating = !alternating;
}
}
TableRow CreatePropertyRow(string propertyName, object propertyValue)
{
return CreatePropertyRow(propertyName, propertyValue, false);
}
TableRow CreatePropertyRow(string propertyName, object value, bool alternating)
{
if (value == null)
return CreateRow(propertyName, null, null, alternating);
else
return CreateRow(propertyName, value.ToString(), value.GetType().FullName, alternating);
}
TableRow CreateRow(string s1, string s2, string s3, bool alternating)
{
TableCell first = new TableCell();
first.CssClass = "l";
first.Text = Abbreviate(s1);
TableCell second = new TableCell();
second.Text = Abbreviate(s2);
TableCell third = new TableCell();
third.Text = Abbreviate(s3);
TableRow row = new TableRow();
if (alternating)
row.CssClass = "s";
row.Cells.Add(first);
row.Cells.Add(second);
row.Cells.Add(third);
return row;
}
private string Abbreviate(string s)
{
if (s == null)
return NullValue;
string retVal = s;
foreach (KeyValuePair<string, string> pair in s_abbreviationMap)
{
//
// We only get one replacement per abbreviation call.
// First one wins.
//
if (retVal.IndexOf(pair.Key) != -1)
{
string replacedValue = string.Format("<span class=\"abbrev\" title=\"{0}\">{1}</span>", pair.Key, pair.Value);
retVal = retVal.Replace(pair.Key, replacedValue);
break;
}
}
return retVal;
}
//
// ASP.NET server side callback
//
protected void GoGetRoles(object sender, EventArgs ea)
{
string[] roles = Roles.Text.Split(';');
UpdateRolesTable(roles);
}
}