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 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
//------------------------------------------------------------------------------
// <copyright file="PassportIdentity.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* PassportIdentity
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.Security {
using System.Text;
using System.Web;
using System.Web.Util;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Globalization;
/// <devdoc>
/// This IIdenty derived class provides access
/// to the Passport profile information contained in the Passport profile cookies.
/// </devdoc>
[Obsolete("This type is obsolete. The Passport authentication product is no longer supported and has been superseded by Live ID.")]
public sealed class PassportIdentity : IIdentity, IDisposable {
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Private Data
private String _Name;
private bool _Authenticated;
private IntPtr _iPassport;
private static int _iPassportVer=0;
private bool _WWWAuthHeaderSet = false;
internal bool WWWAuthHeaderSet { get { return _WWWAuthHeaderSet; }}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Construtor
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)]
public PassportIdentity() {
HttpContext context = HttpContext.Current;
if (_iPassportVer == 0)
_iPassportVer = UnsafeNativeMethods.PassportVersion();
if (_iPassportVer < 3) {
String strTVariable = context.Request.QueryString["t"];
String strPVariable = context.Request.QueryString["p"];
HttpCookie cookieAuth = context.Request.Cookies["MSPAuth"];
HttpCookie cookieProf = context.Request.Cookies["MSPProf"];
HttpCookie cookieProfC = context.Request.Cookies["MSPProfC"];
String strMSPAuthCookie = ((cookieAuth != null && cookieAuth.Value != null) ? cookieAuth.Value : String.Empty);
String strMSPProfCookie = ((cookieProf != null && cookieProf.Value != null) ? cookieProf.Value : String.Empty);
String strMSPProfCCookie = ((cookieProfC != null && cookieProfC.Value != null) ? cookieProfC.Value : String.Empty);
StringBuilder strA = new StringBuilder(1028);
StringBuilder strP = new StringBuilder(1028);
strMSPAuthCookie = HttpUtility.UrlDecode(strMSPAuthCookie);
strMSPProfCookie = HttpUtility.UrlDecode(strMSPProfCookie);
strMSPProfCCookie = HttpUtility.UrlDecode(strMSPProfCCookie);
int iRet = UnsafeNativeMethods.PassportCreate(strTVariable, strPVariable, strMSPAuthCookie,
strMSPProfCookie, strMSPProfCCookie, strA, strP, 1024, ref _iPassport);
if (_iPassport == IntPtr.Zero)
throw new COMException(SR.GetString(SR.Could_not_create_passport_identity), iRet);
String strACookie = UrlEncodeCookie(strA.ToString()); //HttpUtility.AspCompatUrlEncode(strA.ToString());
String strPCookie = UrlEncodeCookie(strP.ToString()); //HttpUtility.AspCompatUrlEncode(strP.ToString());
if (strACookie.Length > 1)
{
context.Response.AppendHeader("Set-Cookie", strACookie);
}
if (strPCookie.Length > 1) {
context.Response.AppendHeader("Set-Cookie", strPCookie);
}
} else {
String strRequestLine = context.Request.HttpMethod + " " +
context.Request.RawUrl + " " +
context.Request.ServerVariables["SERVER_PROTOCOL"] + "\r\n";
StringBuilder szOut = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportCreateHttpRaw(strRequestLine,
context.Request.ServerVariables["ALL_RAW"],
context.Request.IsSecureConnection ? 1 : 0,
szOut, 4090, ref _iPassport);
if (_iPassport == IntPtr.Zero)
throw new COMException(SR.GetString(SR.Could_not_create_passport_identity), iRet);
String strResponseHeaders = szOut.ToString();
SetHeaders(context, strResponseHeaders);
}
_Authenticated = GetIsAuthenticated(-1, -1, -1);
if (_Authenticated == false) {
_Name = String.Empty;
}
}
private void SetHeaders(HttpContext context, String strResponseHeaders) {
for(int iStart = 0; iStart < strResponseHeaders.Length;)
{
int iEnd = strResponseHeaders.IndexOf('\r', iStart);
if (iEnd < 0)
iEnd = strResponseHeaders.Length;
String strCurrentHeader = strResponseHeaders.Substring(iStart, iEnd - iStart);
int iColon = strCurrentHeader.IndexOf(':');
if (iColon > 0)
{
String strHeader = strCurrentHeader.Substring(0, iColon);
String strValue = strCurrentHeader.Substring(iColon+1);
context.Response.AppendHeader(strHeader, strValue);
}
iStart = iEnd + 2;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
~PassportIdentity() {
UnsafeNativeMethods.PassportDestroy(_iPassport);
_iPassport = IntPtr.Zero;
}
private static string UrlEncodeCookie(string strIn) {
if (strIn == null || strIn.Length < 1)
return String.Empty;
int iPos1 = strIn.IndexOf('=');
if (iPos1 < 0)
return HttpUtility.AspCompatUrlEncode(strIn);
iPos1++;
int iPos2 = strIn.IndexOf(';', iPos1);
if (iPos2 < 0)
return HttpUtility.AspCompatUrlEncode(strIn);
string str1 = strIn.Substring(0, iPos1);
string str2 = strIn.Substring(iPos1, iPos2-iPos1);
string str3 = strIn.Substring(iPos2, strIn.Length-iPos2);
return str1 + HttpUtility.AspCompatUrlEncode(str2) + str3;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Properties
/// <devdoc>
/// The name of the identity. In this
/// case, the Passport user name.
/// </devdoc>
public String Name {
get {
if (_Name == null) {
if (_iPassportVer >= 3)
_Name = HexPUID;
else
if (HasProfile("core")) {
_Name = Int32.Parse(this["MemberIDHigh"], CultureInfo.InvariantCulture).ToString("X8", CultureInfo.InvariantCulture) +
Int32.Parse(this["MemberIDLow"], CultureInfo.InvariantCulture).ToString("X8", CultureInfo.InvariantCulture);
} else {
_Name = String.Empty;
}
}
return _Name;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// The type of the identity. In this
/// case, "Passport".
/// </devdoc>
public String AuthenticationType { get { return "Passport";}}
/// <devdoc>
/// <para>True if the user is authenticated against a
/// Passport authority.</para>
/// </devdoc>
public bool IsAuthenticated { get { return _Authenticated;}}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public String this[String strProfileName]
{
get {
Object oValue = GetProfileObject(strProfileName);
if (oValue == null)
return String.Empty;
if (oValue is string)
return(String) oValue;
return oValue.ToString();
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Returns true if the user is authenticated
/// against a Passport authority.
/// </devdoc>
public bool GetIsAuthenticated(int iTimeWindow,
bool bForceLogin,
bool bCheckSecure)
{
return GetIsAuthenticated(iTimeWindow, bForceLogin ? 1 : 0, bCheckSecure ? 10 : 0);
}
public bool GetIsAuthenticated(int iTimeWindow,
int iForceLogin,
int iCheckSecure)
{
int iRet = UnsafeNativeMethods.PassportIsAuthenticated(_iPassport,
iTimeWindow,
iForceLogin,
iCheckSecure);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return (iRet == 0);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Returns Passport profile information for
/// the supplied profile attribute.
/// </devdoc>
public Object GetProfileObject(String strProfileName) {
Object oOut = new Object();
int iRet = UnsafeNativeMethods.PassportGetProfile(_iPassport, strProfileName, out oOut);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return oOut;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Returns an error state associated with the
/// current Passport ticket. See the error property in the Passport documentation
/// for more information.
/// </devdoc>
public int Error {
get {
return UnsafeNativeMethods.PassportGetError(_iPassport);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// True if a connection is coming back from
/// the Passport server (log-in, update, or registration) and if the Passport data
/// contained on the query string is valid.
/// </devdoc>
public bool GetFromNetworkServer {
get {
int iRet = UnsafeNativeMethods.PassportGetFromNetworkServer(_iPassport);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return(iRet == 0);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Returns the Passport domain from the member
/// name string.
/// </devdoc>
public String GetDomainFromMemberName(String strMemberName) {
StringBuilder str = new StringBuilder(1028);
int iRet = UnsafeNativeMethods.PassportDomainFromMemberName(_iPassport, strMemberName, str, 1024);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Determines whether or not a given profile
/// attribute exists in this user's profile.
/// </devdoc>
public bool HasProfile(String strProfile) {
int iRet = UnsafeNativeMethods.PassportHasProfile(_iPassport, strProfile);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return(iRet == 0);
}
public bool HasFlag(int iFlagMask) {
int iRet = UnsafeNativeMethods.PassportHasFlag(_iPassport, iFlagMask);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return(iRet == 0);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public bool HaveConsent(bool bNeedFullConsent, bool bNeedBirthdate) {
int iRet = UnsafeNativeMethods.PassportHasConsent(_iPassport, bNeedFullConsent ? 1 : 0, bNeedBirthdate ? 1 : 0);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return(iRet == 0);
}
public Object GetOption(String strOpt) {
Object vOut = new Object();
int iRet = UnsafeNativeMethods.PassportGetOption(_iPassport, strOpt, out vOut);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return vOut;
}
public void SetOption(String strOpt, Object vOpt) {
int iRet = UnsafeNativeMethods.PassportSetOption(_iPassport, strOpt, vOpt);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
}
public String LogoutURL() {
return LogoutURL(null, null, -1, null, -1);
}
public String LogoutURL(String szReturnURL,
String szCOBrandArgs,
int iLangID,
String strDomain,
int iUseSecureAuth) {
StringBuilder szOut = new StringBuilder(4096);
int iRet = UnsafeNativeMethods.PassportLogoutURL(_iPassport,
szReturnURL,
szCOBrandArgs,
iLangID,
strDomain,
iUseSecureAuth,
szOut,
4096);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return szOut.ToString();
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// True if the Passport member's ticket
/// indicates that they have chosen to save the password on the Passport login page
/// of the last ticket refresh.
/// </devdoc>
public bool HasSavedPassword {
get {
int iRet = UnsafeNativeMethods.PassportGetHasSavedPassword(_iPassport);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return(iRet == 0);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// True if there is a Passport ticket as a
/// cookie on thequery string.
/// </devdoc>
public bool HasTicket {
get {
int iRet = UnsafeNativeMethods.PassportHasTicket(_iPassport);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return(iRet == 0);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Time in seconds since the last ticket was
/// issued or refreshed.
/// </devdoc>
public int TicketAge {
get {
int iRet = UnsafeNativeMethods.PassportGetTicketAge(_iPassport);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return iRet;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// The time is seconds since a member's
/// sign-in to the Passport login server.
/// </devdoc>
public int TimeSinceSignIn {
get {
int iRet = UnsafeNativeMethods.PassportGetTimeSinceSignIn(_iPassport);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return iRet;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Functions
/// <devdoc>
/// <para>Returns an HTML snippet containing an image tag for a Passport link. This is
/// based on the current state of the identity (already signed in, and such).</para>
/// </devdoc>
public String LogoTag() {
return LogoTag(null, -1,
-1, null, -1, -1,
null, -1, -1);
}
public String LogoTag(String strReturnUrl) {
return LogoTag(strReturnUrl, -1,
-1, null, -1, -1,
null, -1, -1);
}
public String LogoTag2() {
return LogoTag2(null, -1,
-1, null, -1, -1,
null, -1, -1);
}
public String LogoTag2(String strReturnUrl) {
return LogoTag2(strReturnUrl, -1,
-1, null, -1, -1,
null, -1, -1);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Similar to LogoTag(), this method returns
/// an HTML snippet for the Passport Logo for the current member
/// </devdoc>
public String LogoTag(String strReturnUrl,
int iTimeWindow,
bool fForceLogin,
String strCoBrandedArgs,
int iLangID,
bool fSecure,
String strNameSpace,
int iKPP,
bool bUseSecureAuth)
{
return LogoTag(
strReturnUrl,
iTimeWindow,
(fForceLogin ? 1 : 0),
strCoBrandedArgs,
iLangID,
fSecure ? 1 : 0,
strNameSpace,
iKPP,
bUseSecureAuth ? 10 : 0);
}
public String LogoTag(String strReturnUrl,
int iTimeWindow,
int iForceLogin,
String strCoBrandedArgs,
int iLangID,
int iSecure,
String strNameSpace,
int iKPP,
int iUseSecureAuth)
{
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportLogoTag(_iPassport,
strReturnUrl,
iTimeWindow,
iForceLogin,
strCoBrandedArgs,
iLangID,
iSecure,
strNameSpace,
iKPP,
iUseSecureAuth,
str,
4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
public String LogoTag2(String strReturnUrl,
int iTimeWindow,
bool fForceLogin,
String strCoBrandedArgs,
int iLangID,
bool fSecure,
String strNameSpace,
int iKPP,
bool bUseSecureAuth)
{
return LogoTag2(
strReturnUrl,
iTimeWindow,
fForceLogin ? 1 : 0,
strCoBrandedArgs,
iLangID,
fSecure ? 1 : 0,
strNameSpace,
iKPP,
bUseSecureAuth ? 10 : 0);
}
public String LogoTag2(String strReturnUrl,
int iTimeWindow,
int iForceLogin,
String strCoBrandedArgs,
int iLangID,
int iSecure,
String strNameSpace,
int iKPP,
int iUseSecureAuth)
{
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportLogoTag2(_iPassport,
strReturnUrl,
iTimeWindow,
iForceLogin,
strCoBrandedArgs,
iLangID,
iSecure,
strNameSpace,
iKPP,
iUseSecureAuth,
str,
4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// </devdoc>
public String AuthUrl() {
return AuthUrl(null, -1, -1, null, -1, null, -1, -1);
}
public String AuthUrl(String strReturnUrl) {
return AuthUrl(strReturnUrl, -1, -1, null, -1, null, -1, -1);
}
public String AuthUrl2() {
return AuthUrl2(null, -1, -1, null, -1, null, -1, -1);
}
public String AuthUrl2(String strReturnUrl) {
return AuthUrl2(strReturnUrl, -1, -1, null, -1, null, -1, -1);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// <para>Similar to AuthUrl(). Returns the authentication server URL for
/// a member</para>
/// </devdoc>
public String AuthUrl (String strReturnUrl,
int iTimeWindow,
bool fForceLogin,
String strCoBrandedArgs,
int iLangID,
String strNameSpace,
int iKPP,
bool bUseSecureAuth)
{
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportAuthURL(_iPassport,
strReturnUrl,
iTimeWindow,
(fForceLogin ? 1 : 0),
strCoBrandedArgs,
iLangID,
strNameSpace,
iKPP,
bUseSecureAuth ? 10 : 0,
str,
4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
public String AuthUrl2 (String strReturnUrl,
int iTimeWindow,
bool fForceLogin,
String strCoBrandedArgs,
int iLangID,
String strNameSpace,
int iKPP,
bool bUseSecureAuth)
{
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportAuthURL2(_iPassport,
strReturnUrl,
iTimeWindow,
(fForceLogin ? 1 : 0),
strCoBrandedArgs,
iLangID,
strNameSpace,
iKPP,
bUseSecureAuth ? 10 : 0,
str,
4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
public String AuthUrl (String strReturnUrl,
int iTimeWindow,
int iForceLogin,
String strCoBrandedArgs,
int iLangID,
String strNameSpace,
int iKPP,
int iUseSecureAuth)
{
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportAuthURL(_iPassport,
strReturnUrl,
iTimeWindow,
iForceLogin,
strCoBrandedArgs,
iLangID,
strNameSpace,
iKPP,
iUseSecureAuth,
str,
4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
public String AuthUrl2 (String strReturnUrl,
int iTimeWindow,
int iForceLogin,
String strCoBrandedArgs,
int iLangID,
String strNameSpace,
int iKPP,
int iUseSecureAuth) {
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportAuthURL2(_iPassport,
strReturnUrl,
iTimeWindow,
iForceLogin,
strCoBrandedArgs,
iLangID,
strNameSpace,
iKPP,
iUseSecureAuth,
str,
4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return str.ToString();
}
public int LoginUser(
String szRetURL,
int iTimeWindow,
bool fForceLogin,
String szCOBrandArgs,
int iLangID,
String strNameSpace,
int iKPP,
bool fUseSecureAuth,
object oExtraParams) {
return LoginUser(
szRetURL,
iTimeWindow,
fForceLogin ? 1 : 0,
szCOBrandArgs,
iLangID,
strNameSpace,
iKPP,
fUseSecureAuth ? 10 : 0,
oExtraParams);
}
public int LoginUser(
String szRetURL,
int iTimeWindow,
int fForceLogin,
String szCOBrandArgs,
int iLangID,
String strNameSpace,
int iKPP,
int iUseSecureAuth,
object oExtraParams) {
String str = GetLoginChallenge(szRetURL, iTimeWindow, fForceLogin, szCOBrandArgs,
iLangID, strNameSpace, iKPP, iUseSecureAuth, oExtraParams);
if (str == null || str.Length < 1)
return -1;
HttpContext context = HttpContext.Current;
SetHeaders(context, str);
_WWWAuthHeaderSet = true;
str = context.Request.Headers["Accept-Auth"];
if (str != null && str.Length > 0 && str.IndexOf("Passport", StringComparison.Ordinal) >= 0) {
context.Response.StatusCode = 401;
context.Response.End();
return 0;
}
str = AuthUrl(szRetURL, iTimeWindow, fForceLogin, szCOBrandArgs,
iLangID, strNameSpace, iKPP, iUseSecureAuth);
if (!String.IsNullOrEmpty(str)) {
context.Response.Redirect(str, false);
return 0;
}
return -1;
}
public int LoginUser() {
return LoginUser(null, -1, -1, null, -1, null, -1, -1, null);
}
public int LoginUser(String strReturnUrl) {
return LoginUser(strReturnUrl, -1, -1, null, -1, null, -1, -1, null);
}
public String GetLoginChallenge() {
return GetLoginChallenge(null, -1, -1, null, -1, null, -1, -1, null);
}
public String GetLoginChallenge(String strReturnUrl) {
return GetLoginChallenge(strReturnUrl, -1, -1, null, -1, null, -1, -1, null);
}
public String GetLoginChallenge(
String szRetURL,
int iTimeWindow,
int fForceLogin,
String szCOBrandArgs,
int iLangID,
String strNameSpace,
int iKPP,
int iUseSecureAuth,
object oExtraParams) {
StringBuilder str = new StringBuilder(4092);
int iRet = UnsafeNativeMethods.PassportGetLoginChallenge(
_iPassport,
szRetURL,
iTimeWindow,
fForceLogin,
szCOBrandArgs,
iLangID,
strNameSpace,
iKPP,
iUseSecureAuth,
oExtraParams,
str, 4090);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
String strRet = str.ToString();
if (strRet != null && !StringUtil.StringStartsWith(strRet, "WWW-Authenticate"))
strRet = "WWW-Authenticate: " + strRet;
return strRet;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Provides information for a Passport domain
/// by querying the Passport CCD for the requested domain attribute.
/// </devdoc>
public String GetDomainAttribute(String strAttribute, int iLCID, String strDomain) {
StringBuilder str = new StringBuilder(1028);
int iRet = UnsafeNativeMethods.PassportGetDomainAttribute(_iPassport, strAttribute, iLCID, strDomain, str, 1024);
if (iRet >= 0)
return str.ToString();
else
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
}
public Object Ticket(String strAttribute) {
Object oOut = new Object();
int iRet = UnsafeNativeMethods.PassportTicket(_iPassport, strAttribute, out oOut);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return oOut;
}
public Object GetCurrentConfig(String strAttribute) {
Object oOut = new Object();
int iRet = UnsafeNativeMethods.PassportGetCurrentConfig(_iPassport, strAttribute, out oOut);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return oOut;
}
public String HexPUID {
get {
StringBuilder str = new StringBuilder(1024);
int iRet = UnsafeNativeMethods.PassportHexPUID(_iPassport, str, 1024);
if (iRet >= 0)
return str.ToString();
else
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
}
}
/// <internalonly/>
void IDisposable.Dispose()
{
if (_iPassport != IntPtr.Zero)
UnsafeNativeMethods.PassportDestroy(_iPassport);
_iPassport = IntPtr.Zero;
GC.SuppressFinalize(this);
}
/// <devdoc>
/// Signs out the given Passport member from
/// their current session.
/// </devdoc>
public static void SignOut(String strSignOutDotGifFileName) {
HttpContext context = HttpContext.Current;
String [] sCookieNames = {"MSPAuth", "MSPProf", "MSPConsent", "MSPSecAuth", "MSPProfC"};
String [] sCookieDomains = {"TicketDomain", "TicketDomain", "ProfileDomain", "SecureDomain", "TicketDomain"};
String [] sCookiePaths = {"TicketPath", "TicketPath", "ProfilePath", "SecurePath", "TicketPath"};
String [] sCookieDomainsV = new String[5];
String [] sCookiePathsV = new String[5];
PassportIdentity pi = null;
int iter = 0;
////////////////////////////////////////////////////////////
// Step 1: Clear all headers
context.Response.ClearHeaders();
////////////////////////////////////////////////////////////
// Step 2: Get passport config information (if using Passport 2.0 sdk)
try {
if (context.User.Identity is PassportIdentity) {
pi = (PassportIdentity) context.User.Identity;
} else {
pi = new PassportIdentity();
}
if (pi != null && _iPassportVer >= 3) {
// Get Domains
for(iter=0; iter<5; iter++) {
Object obj = pi.GetCurrentConfig(sCookieDomains[iter]);
if (obj != null && (obj is String))
sCookieDomainsV[iter] = (String) obj;
}
// Get Paths
for(iter=0; iter<5; iter++) {
Object obj = pi.GetCurrentConfig(sCookiePaths[iter]);
if (obj != null && (obj is String))
sCookiePathsV[iter] = (String) obj;
}
}
}
catch {
// ---- exceptions
}
////////////////////////////////////////////////////////////
// Step 3: Add cookies
for(iter=0; iter<5; iter++) {
HttpCookie cookie = new HttpCookie(sCookieNames[iter], String.Empty);
cookie.Expires = new DateTime(1998, 1, 1);
if (sCookieDomainsV[iter] != null && sCookieDomainsV[iter].Length > 0)
cookie.Domain = sCookieDomainsV[iter];
if (sCookiePathsV[iter] != null && sCookiePathsV[iter].Length > 0)
cookie.Path = sCookiePathsV[iter];
else
cookie.Path = "/";
context.Response.Cookies.Add(cookie);
}
// context.Response.AppendHeader("P3P", "CP=\"TST\"");
////////////////////////////////////////////////////////////
// Step 4: Add no-cache headers
context.Response.Expires = -1;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AppendHeader("Pragma", "no-cache");
context.Response.ContentType = "image/gif";
////////////////////////////////////////////////////////////
// Step 5: Write Image file
context.Response.WriteFile(strSignOutDotGifFileName);
////////////////////////////////////////////////////////////
// Step 6: Mobile device support: Redirect to the "ru" in the QS (if present)
String strRU = context.Request.QueryString["ru"];
if (strRU != null && strRU.Length > 1)
context.Response.Redirect(strRU, false);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// <devdoc>
/// Encrypts data using the Passport
/// participant keycfor the current site. Maximum input size is 2045 characters.
/// </devdoc>
static public String Encrypt(String strData) {
return CallPassportCryptFunction(0, strData);
}
/// <devdoc>
/// Decrypts data using the Passport
/// participant key for the current site.
/// </devdoc>
static public String Decrypt(String strData) {
return CallPassportCryptFunction(1, strData);
}
static public String Compress(String strData) {
return CallPassportCryptFunction(2, strData);
}
static public String Decompress(String strData) {
return CallPassportCryptFunction(3, strData);
}
static public int CryptPutHost(String strHost) {
int iRet = UnsafeNativeMethods.PassportCryptPut(0, strHost);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return iRet;
}
static public int CryptPutSite(String strSite) {
int iRet = UnsafeNativeMethods.PassportCryptPut(1, strSite);
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return iRet;
}
static public bool CryptIsValid() {
int iRet = UnsafeNativeMethods.PassportCryptIsValid();
if (iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
return (iRet == 0);
}
static private String CallPassportCryptFunction(int iFunctionID, String strData) {
int iRet = 0;
int iSize = ((strData == null || strData.Length < 512) ? 512 : strData.Length);
do {
iSize *= 2;
StringBuilder str = new StringBuilder(iSize);
iRet = UnsafeNativeMethods.PassportCrypt(iFunctionID, strData, str, iSize);
if (iRet == 0) // Worked
return str.ToString();
if (iRet != HResults.E_INSUFFICIENT_BUFFER && iRet < 0)
throw new COMException(SR.GetString(SR.Passport_method_failed), iRet);
}
while ( iRet == HResults.E_INSUFFICIENT_BUFFER &&
iSize < 10*1024*1024 ); // Less than 10MB
return null;
}
}
}
|