rprimrose

Untitled

Oct 22nd, 2013
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-----------------------------------------------------------------------------
  2. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5. // PARTICULAR PURPOSE.
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace MyApp.Server.Web.TestSts
  9. {
  10.     using System;
  11.     using System.Collections.Generic;
  12.     using System.Configuration;
  13.     using System.IdentityModel.Services;
  14.     using System.Security.Claims;
  15.     using System.Security.Principal;
  16.     using System.Web.Security;
  17.     using System.Web.UI;
  18.  
  19.     /// <summary>
  20.     ///     The Default Page Class
  21.     /// </summary>
  22.     public partial class _Default : Page
  23.     {
  24.         /// <summary>
  25.         /// The page_ load.
  26.         /// </summary>
  27.         /// <param name="sender">
  28.         /// The sender.
  29.         /// </param>
  30.         /// <param name="e">
  31.         /// The e.
  32.         /// </param>
  33.         protected void Page_Load(object sender, EventArgs e)
  34.         {
  35.             string action = Request.QueryString["wa"];
  36.  
  37.             if (action == "wsignout1.0")
  38.             {
  39.                 ClaimsPrincipal principal = User as ClaimsPrincipal;
  40.  
  41.                 if (principal == null)
  42.                 {
  43.                     // This is a test STS which is not tracking authenticated users
  44.                     // Create a temporary principal as a workaround for the process request action
  45.                     principal = new ClaimsPrincipal(new GenericIdentity("Temp"));
  46.                 }
  47.  
  48.                 FederatedPassiveSecurityTokenServiceOperations.ProcessRequest(
  49.                     Request,
  50.                     principal,
  51.                     CustomSecurityTokenServiceConfiguration.Current.CreateSecurityTokenService(),
  52.                     Response);
  53.             }
  54.  
  55.             if (!IsPostBack)
  56.             {
  57.                 txtUserName.Text = "TestUser";
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// The btn submit_ click.
  63.         /// </summary>
  64.         /// <param name="sender">
  65.         /// The sender.
  66.         /// </param>
  67.         /// <param name="e">
  68.         /// The e.
  69.         /// </param>
  70.         protected void btnSubmit_Click(object sender, EventArgs e)
  71.         {
  72.             var claims = new List<Claim>
  73.             {
  74.                 new Claim(ClaimsIdentity.DefaultNameClaimType, txtUserName.Text)
  75.             };
  76.  
  77.             if (chkIsAdmin.Checked)
  78.             {
  79.                 claims.Add(new Claim(ClaimsIdentity.DefaultRoleClaimType, "Administrator"));
  80.             }
  81.  
  82.             var additionalRoles = txtRoles.Text;
  83.  
  84.             if (string.IsNullOrWhiteSpace(additionalRoles) == false)
  85.             {
  86.                 var roles = additionalRoles.Split(
  87.                     new[]
  88.                     {
  89.                         Environment.NewLine
  90.                     },
  91.                     StringSplitOptions.RemoveEmptyEntries);
  92.  
  93.                 foreach (string role in roles)
  94.                 {
  95.                     if (string.IsNullOrWhiteSpace(role))
  96.                     {
  97.                         continue;
  98.                     }
  99.  
  100.                     claims.Add(new Claim(ClaimsIdentity.DefaultRoleClaimType, role));
  101.                 }
  102.             }
  103.  
  104.             var identity = new ClaimsIdentity(claims, ConfigurationManager.AppSettings["IssuerName"]);
  105.  
  106.             var principal = new ClaimsPrincipal(identity);
  107.  
  108.             FederatedPassiveSecurityTokenServiceOperations.ProcessRequest(
  109.                 Request,
  110.                 principal,
  111.                 CustomSecurityTokenServiceConfiguration.Current.CreateSecurityTokenService(),
  112.                 Response);
  113.         }
  114.     }
  115. }
RAW Paste Data