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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
|
//------------------------------------------------------------------------------
// <copyright file="AuthStoreRoleProvider.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Security
{
using System.Web;
using System.Web.Configuration;
using System.Web.Util;
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;
using System.Globalization;
using System.Runtime.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using System.Configuration.Provider;
using System.Configuration;
using System.Data.OleDb;
using System.Reflection;
using System.Web.Hosting;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public class AuthorizationStoreRoleProvider : RoleProvider
{
////////////////////////////////////////////////////////////
// Public properties
public override string ApplicationName
{
get { return _AppName; }
set {
if (_AppName != value) {
if ( value.Length > 256 )
throw new ProviderException( SR.GetString(SR.Provider_application_name_too_long) );
_AppName = value;
_InitAppDone = false;
}
}
}
public string ScopeName
{
get { return _ScopeName; }
set {
if( _ScopeName != value ) {
_ScopeName = value;
_InitAppDone = false;
}
}
}
public int CacheRefreshInterval
{
get{ return _CacheRefreshInterval; }
}
////////////////////////////////////////////////////////////
// Public Methods
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override void Initialize(string name, NameValueCollection config)
{
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, SR.Feature_not_supported_at_this_level);
if (String.IsNullOrEmpty(name))
name = "AuthorizationStoreRoleProvider";
if (config == null)
throw new ArgumentNullException("config");
if (string.IsNullOrEmpty(config["description"])) {
config.Remove("description");
config.Add("description", SR.GetString(SR.RoleAuthStoreProvider_description));
}
base.Initialize(name, config);
_CacheRefreshInterval = SecUtility.GetIntValue( config, "cacheRefreshInterval", 60, false, 0 );
_ScopeName = config["scopeName"];
if (_ScopeName != null && _ScopeName.Length == 0)
_ScopeName = null;
_ConnectionString = config["connectionStringName"];
if (_ConnectionString == null || _ConnectionString.Length < 1)
throw new ProviderException(SR.GetString(SR.Connection_name_not_specified));
ConnectionStringsSection sec1 = null;
sec1 = RuntimeConfig.GetAppConfig().ConnectionStrings;
ConnectionStringSettings connObj = sec1.ConnectionStrings[_ConnectionString];
if (connObj == null)
throw new ProviderException(SR.GetString(SR.Connection_string_not_found, _ConnectionString));
if (string.IsNullOrEmpty(connObj.ConnectionString))
throw new ProviderException(SR.GetString(SR.Connection_string_not_found, _ConnectionString));
_ConnectionString = connObj.ConnectionString;
_AppName = config["applicationName"];
if (string.IsNullOrEmpty(_AppName))
_AppName = SecUtility.GetDefaultAppName();
if( _AppName.Length > 256 )
{
throw new ProviderException(SR.GetString(SR.Provider_application_name_too_long));
}
config.Remove("connectionStringName");
config.Remove("cacheRefreshInterval");
config.Remove("applicationName");
config.Remove("scopeName");
if (config.Count > 0)
{
string attribUnrecognized = config.GetKey(0);
if (!String.IsNullOrEmpty(attribUnrecognized))
throw new ProviderException(SR.GetString(SR.Provider_unrecognized_attribute, attribUnrecognized));
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override bool IsUserInRole(string username, string roleName)
{
SecUtility.CheckParameter(ref username, true, false, true, 0, "username");
if (username.Length < 1)
return false;
SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
bool foundRole = IsUserInRoleCore(username, roleName);
return foundRole;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override string[] GetRolesForUser(string username)
{
SecUtility.CheckParameter(ref username, true, false, true, 0, "username");
if (username.Length < 1)
return new string[0];
string[] allRoles = GetRolesForUserCore(username);
return allRoles;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override void CreateRole(string roleName)
{
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Medium, SR.API_not_supported_at_this_level);
SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
InitApp();
object[] args = new object[2];
args[0] = roleName;
args[1] = null;
object role = CallMethod(_ObjAzScope != null ? _ObjAzScope : _ObjAzApplication, "CreateRole", args);
args[0] = 0;
args[1] = null;
try {
try {
CallMethod(role, "Submit", args);
} finally {
//
// Release the handle to the underlying object
//
Marshal.FinalReleaseComObject(role);
}
} catch {
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Medium, SR.API_not_supported_at_this_level);
SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
InitApp();
if (throwOnPopulatedRole)
{
string[] users;
try
{
users = GetUsersInRole(roleName);
}
catch
{
return false;
}
if (users.Length != 0)
throw new ProviderException(SR.GetString(SR.Role_is_not_empty));
}
object[] args = new object[2];
args[0] = roleName;
args[1] = null;
CallMethod(_ObjAzScope != null ? _ObjAzScope : _ObjAzApplication, "DeleteRole", args);
args[0] = 0;
args[1] = null;
CallMethod(_ObjAzScope != null ? _ObjAzScope : _ObjAzApplication, "Submit", args);
return true;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override bool RoleExists(string roleName)
{
SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
bool found = false;
object role = null;
try {
role = GetRole(roleName);
found = (role != null);
} catch (TargetInvocationException e) {
// "Element not found" error is expected
COMException ce = (e.InnerException as COMException);
if (ce != null && (uint)ce.ErrorCode == 0x80070490) {
return false;
}
throw;
} finally {
if (role != null)
Marshal.FinalReleaseComObject(role);
}
return found;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Medium, SR.API_not_supported_at_this_level);
SecUtility.CheckArrayParameter(ref roleNames,
true,
true,
true,
0,
"roleNames");
SecUtility.CheckArrayParameter( ref usernames,
true,
true,
true,
0,
"usernames");
int index = 0;
object[] args = new object[ 2 ];
object[] roles = new object[ roleNames.Length ];
foreach( string rolename in roleNames )
{
roles[ index++ ] = GetRole( rolename );
}
try {
try {
foreach (object role in roles) {
foreach (string username in usernames) {
args[0] = username;
args[1] = null;
CallMethod(role, "AddMemberName", args);
}
}
foreach (object role in roles) {
args[0] = 0;
args[1] = null;
CallMethod(role, "Submit", args);
}
} finally {
foreach (object role in roles) {
Marshal.FinalReleaseComObject(role);
}
}
} catch {
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
{
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Medium, SR.API_not_supported_at_this_level);
SecUtility.CheckArrayParameter(ref roleNames,
true,
true,
true,
0,
"roleNames");
SecUtility.CheckArrayParameter( ref userNames,
true,
true,
true,
0,
"userNames");
int index = 0;
object[] args = new object[ 2 ];
object[] roles = new object[ roleNames.Length ];
foreach( string rolename in roleNames )
{
roles[ index++ ] = GetRole( rolename );
}
try {
try {
foreach (object role in roles) {
foreach (string username in userNames) {
args[0] = username;
args[1] = null;
CallMethod(role, "DeleteMemberName", args);
}
}
foreach (object role in roles) {
args[0] = 0;
args[1] = null;
CallMethod(role, "Submit", args);
}
} finally {
foreach (object role in roles) {
Marshal.FinalReleaseComObject(role);
}
}
} catch {
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override string[] GetUsersInRole(string roleName)
{
SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
object role = GetRole(roleName);
object memberNames;
try {
try {
memberNames = CallProperty(role, "MembersName", null);
} finally {
//
// Release the handle to the underlying object
//
Marshal.FinalReleaseComObject(role);
}
} catch {
throw;
}
StringCollection userNameCollection = new StringCollection();
try
{
if ( HostingEnvironment.IsHosted && _XmlFileName != null )
{
InternalSecurityPermissions.Unrestricted.Assert();
}
try
{
IEnumerable allUsers = (IEnumerable)memberNames;
foreach (object objUserName in allUsers)
userNameCollection.Add((string)objUserName);
}
finally
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
CodeAccessPermission.RevertAssert();
}
}
}
catch
{
throw;
}
string [] usersArray = new string[userNameCollection.Count];
userNameCollection.CopyTo(usersArray, 0);
return usersArray;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override string[] GetAllRoles()
{
InitApp();
object objAllRoles = CallProperty(_ObjAzScope != null ? _ObjAzScope : _ObjAzApplication, "Roles", null);
StringCollection roleNameCollection = new StringCollection();
try
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
InternalSecurityPermissions.Unrestricted.Assert();
}
try
{
IEnumerable allRoles = (IEnumerable)objAllRoles;
foreach (object role in allRoles)
{
string name = (string)CallProperty(role, "Name", null);
roleNameCollection.Add(name);
}
}
finally
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
CodeAccessPermission.RevertAssert();
}
}
}
catch
{
throw;
}
string[] rolesArray = new string[roleNameCollection.Count];
roleNameCollection.CopyTo(rolesArray, 0);
return rolesArray;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Private Methods and data
private string _AppName;
private string _ConnectionString;
private int _CacheRefreshInterval;
private string _ScopeName;
private object _ObjAzApplication;
private bool _InitAppDone;
private object _ObjAzScope;
private DateTime _LastUpdateCacheDate;
private object _ObjAzAuthorizationStoreClass;
private bool _NewAuthInterface;
private string _XmlFileName;
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private object CallMethod(object objectToCallOn, string methodName, object[] args)
{
if( HostingEnvironment.IsHosted && _XmlFileName != null) {
InternalSecurityPermissions.Unrestricted.Assert();
}
try {
using (new ApplicationImpersonationContext()) {
return objectToCallOn.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null, objectToCallOn, args, CultureInfo.InvariantCulture);
}
} catch {
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private object CallProperty(object objectToCallOn, string propName, object[] args)
{
if (HostingEnvironment.IsHosted && _XmlFileName != null) {
InternalSecurityPermissions.Unrestricted.Assert();
}
try {
using (new ApplicationImpersonationContext()) {
return objectToCallOn.GetType().InvokeMember(propName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
null, objectToCallOn, args, CultureInfo.InvariantCulture);
}
} catch {
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private void InitApp()
{
try {
using (new ApplicationImpersonationContext()) {
if (_InitAppDone) {
if (DateTime.Now > _LastUpdateCacheDate.AddMinutes(CacheRefreshInterval)) {
_LastUpdateCacheDate = DateTime.Now;
CallMethod(_ObjAzAuthorizationStoreClass, "UpdateCache", null);
}
return;
}
lock (this) {
if (_InitAppDone)
return;
if (_ConnectionString.ToLower(CultureInfo.InvariantCulture).StartsWith("msxml://", StringComparison.Ordinal)) {
if (_ConnectionString.Contains("/~/")) {
string appPath = null;
if (HostingEnvironment.IsHosted)
appPath = HttpRuntime.AppDomainAppPath;
else {
#if !FEATURE_PAL // FEATURE_PAL does not enable the ProcessModule class
Process p = Process.GetCurrentProcess();
ProcessModule pm = (p != null ? p.MainModule : null);
string exeName = (pm != null ? pm.FileName : null);
if (exeName != null)
appPath = System.IO.Path.GetDirectoryName(exeName);
#endif // !FEATURE_PAL
if (appPath == null || appPath.Length < 1)
appPath = Environment.CurrentDirectory;
}
appPath = appPath.Replace('\\', '/');
_ConnectionString = _ConnectionString.Replace("~", appPath);
}
string fileName = _ConnectionString.Substring("msxml://".Length).Replace('/', '\\');
if( HostingEnvironment.IsHosted )
{
HttpRuntime.CheckFilePermission( fileName, false );
}
if (!FileUtil.FileExists(fileName)) {
throw new FileNotFoundException(SR.GetString(SR.AuthStore_policy_file_not_found,
HttpRuntime.GetSafePath(fileName)));
}
_XmlFileName = fileName;
}
Type typeAzAuthorizationStoreClass = null;
try {
_NewAuthInterface = true;
typeAzAuthorizationStoreClass = Type.GetType("Microsoft.Interop.Security.AzRoles.AzAuthorizationStoreClass, Microsoft.Interop.Security.AzRoles, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
false /*throwOnError*/);
if (typeAzAuthorizationStoreClass == null)
typeAzAuthorizationStoreClass = Type.GetType("Microsoft.Interop.Security.AzRoles.AzAuthorizationStoreClass, Microsoft.Interop.Security.AzRoles, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
false /*throwOnError*/);
if (typeAzAuthorizationStoreClass == null) {
_NewAuthInterface = false;
typeAzAuthorizationStoreClass = Type.GetType("Microsoft.Interop.Security.AzRoles.AzAuthorizationStoreClass, Microsoft.Interop.Security.AzRoles, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
true /*throwOnError*/);
}
}
catch (FileNotFoundException e) {
HttpContext context = HttpContext.Current;
if (context == null)
{
throw new ProviderException(SR.GetString(SR.AuthStoreNotInstalled_Title), e);
}
context.Response.Clear();
context.Response.StatusCode = 500;
context.Response.Write(AuthStoreErrorFormatter.GetErrorText());
context.Response.End();
}
if ( HostingEnvironment.IsHosted && _XmlFileName != null )
{
InternalSecurityPermissions.Unrestricted.Assert();
}
_ObjAzAuthorizationStoreClass = Activator.CreateInstance(typeAzAuthorizationStoreClass);
object[] args = new object[] { 0, _ConnectionString, null };
CallMethod(_ObjAzAuthorizationStoreClass, "Initialize", args);
args = new object[2];
args[0] = _AppName;
args[1] = null;
if(_NewAuthInterface)
{
_ObjAzApplication = CallMethod(_ObjAzAuthorizationStoreClass, "OpenApplication2", args);
}
else
{
_ObjAzApplication = CallMethod(_ObjAzAuthorizationStoreClass, "OpenApplication", args);
}
if (_ObjAzApplication == null)
throw new ProviderException(SR.GetString(SR.AuthStore_Application_not_found));
_ObjAzScope = null;
if (!string.IsNullOrEmpty(_ScopeName)) {
args[0] = _ScopeName;
args[1] = null;
_ObjAzScope = CallMethod(_ObjAzApplication, "OpenScope", args);
if (_ObjAzScope == null)
throw new ProviderException(SR.GetString(SR.AuthStore_Scope_not_found));
}
_LastUpdateCacheDate = DateTime.Now;
_InitAppDone = true;
}
}
} catch {
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
[PermissionSet(SecurityAction.Assert, Unrestricted = true)]
private IntPtr GetWindowsTokenWithAssert(string userName)
{
if (HostingEnvironment.IsHosted)
{
HttpContext context = HttpContext.Current;
if (context != null && context.User != null && context.User.Identity != null && context.User.Identity is WindowsIdentity &&
StringUtil.EqualsIgnoreCase(userName, context.User.Identity.Name))
{
return ((WindowsIdentity)context.User.Identity).Token;
}
}
IPrincipal user = Thread.CurrentPrincipal;
if (user != null && user.Identity != null && user.Identity is WindowsIdentity &&
StringUtil.EqualsIgnoreCase(userName, user.Identity.Name))
{
return ((WindowsIdentity)user.Identity).Token;
}
return IntPtr.Zero;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private object GetClientContext(string userName)
{
InitApp();
IntPtr token = GetWindowsTokenWithAssert(userName);
if (token != IntPtr.Zero)
return GetClientContextFromToken(token);
else
return GetClientContextFromName(userName);
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private object GetClientContextFromToken(IntPtr token)
{
if (_NewAuthInterface)
{
object [] args = new object[3];
args[0] = ( UInt32 )token;
args[1] = 0;
args[2] = null;
return CallMethod(_ObjAzApplication, "InitializeClientContextFromToken2", args);
}
else
{
object [] args = new object[2];
args[0] = ( UInt64 )token;
args[1] = null;
return CallMethod(_ObjAzApplication, "InitializeClientContextFromToken", args);
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private object GetClientContextFromName(string userName)
{
string[] names = userName.Split(new char[] { '\\' });
string domain = null;
if (names.Length > 1) {
domain = names[0];
userName = names[1];
}
object [] args = new object[3];
args[0] = userName;
args[1] = domain;
args[2] = null;
return CallMethod(_ObjAzApplication, "InitializeClientContextFromName", args);
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private bool IsUserInRoleCore(string username, string roleName)
{
object objClientContext = GetClientContext(username);
if (objClientContext == null)
return false;
object objAllRoles = CallMethod(objClientContext, "GetRoles", new object[] { _ScopeName });
if (objAllRoles == null || !(objAllRoles is IEnumerable))
return false;
try
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
InternalSecurityPermissions.Unrestricted.Assert();
}
try
{
IEnumerable allRoles = (IEnumerable)objAllRoles;
foreach (object objRoleName in allRoles)
{
string strRoleName = (string)objRoleName;
if (strRoleName != null && StringUtil.EqualsIgnoreCase(strRoleName, roleName))
return true;
}
return false;
}
finally
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
CodeAccessPermission.RevertAssert();
}
}
}
catch
{
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private string[] GetRolesForUserCore(string username)
{
object objClientContext = GetClientContext(username);
if (objClientContext == null)
return new string[0];
object objAllRoles = CallMethod(objClientContext, "GetRoles", new object[] { _ScopeName });
if (objAllRoles == null || !(objAllRoles is IEnumerable))
return new string[0];
StringCollection roleNameCollection = new StringCollection();
try
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
InternalSecurityPermissions.Unrestricted.Assert();
}
try
{
IEnumerable allRoles = (IEnumerable)objAllRoles;
foreach (object objRoleName in allRoles)
{
string strRoleName = (string)objRoleName;
if (strRoleName != null)
roleNameCollection.Add(strRoleName);
}
}
finally
{
if( HostingEnvironment.IsHosted && _XmlFileName != null )
{
CodeAccessPermission.RevertAssert();
}
}
}
catch
{
throw;
}
string[] rolesArray = new string[roleNameCollection.Count];
roleNameCollection.CopyTo(rolesArray, 0);
return rolesArray;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private object GetRole(string roleName)
{
InitApp();
object[] args = new object[2];
args[0] = roleName;
args[1] = null;
return CallMethod(_ObjAzScope != null ? _ObjAzScope : _ObjAzApplication, "OpenRole", args);
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// ErrorFormatter for generating adaptive error for Authorization
// Store Role Provider
internal sealed class AuthStoreErrorFormatter : ErrorFormatter
{
private static String s_errMsg = null;
private static Object s_Lock = new Object();
internal AuthStoreErrorFormatter()
{
}
internal static String GetErrorText()
{
if( s_errMsg != null )
{
return s_errMsg;
}
lock( s_Lock )
{
if( s_errMsg != null )
{
return s_errMsg;
}
AuthStoreErrorFormatter errFormatter = new AuthStoreErrorFormatter();
s_errMsg = errFormatter.GetErrorMessage();
}
return s_errMsg;
}
protected override string ErrorTitle
{
get
{
return SR.GetString(SR.AuthStoreNotInstalled_Title) ;
}
}
protected override string Description
{
get
{
return SR.GetString(SR.AuthStoreNotInstalled_Description) ;
}
}
protected override string MiscSectionTitle
{
get
{
return null;
}
}
protected override string MiscSectionContent
{
get
{
return null;
}
}
protected override string ColoredSquareTitle
{
get
{
return null;
}
}
protected override string ColoredSquareContent
{
get
{
return null;
}
}
protected override bool ShowSourceFileInfo
{
get
{
return false;
}
}
}
}
|