using System.Data;
using System.Configuration;
using System.Collections;
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 Microsoft.SharePoint.Portal;
using Microsoft.SharePoint.Portal.SingleSignon;
namespace SSODEMO
{
public partial class SsoToGMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IntPtr pUserName = IntPtr.Zero;
IntPtr pPassword = IntPtr.Zero;
try
{
ISsoProvider isso = SsoProviderFactory.GetSsoProvider();
SsoCredentials myCreds = isso.GetCredentials("GoogleMail");
pUserName = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(myCreds.UserName);
String userName = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pUserName);
pPassword = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(myCreds.Password);
String Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pPassword);
string strHTML = "";
strHTML += "<html> ";
strHTML += "<body > ";
strHTML += "<form id='myform' name='myform' method='POST'
action='https://www.google.com/accounts/ServiceLoginAuth' > ";
strHTML += "<input type='hidden' name='continue'
value='http://mail.google.com/mail?ui=html&zy=l'> ";
strHTML += "<input type='hidden' name='service' value='mail'> ";
strHTML += "<input type='hidden' id='Email' name='Email' value='" + userName + "' > ";
strHTML += "<input type='hidden' id='Passwd' name='Passwd' value='" + Password + "' >";
strHTML += "</form> ";
strHTML += "<script > ";
strHTML += " window.onload=myform.submit(); ";
strHTML += " document.all('Email').value='';document.all('Passwd').value='';";
strHTML += "</script> ";
strHTML += "</body> ";
strHTML += "</html> ";
Response.Write(strHTML);
}
catch (SingleSignonCredsNotFoundException ssoe)
{
if (SSOReturnCodes.SSO_E_CREDS_NOT_FOUND == ssoe.LastErrorCode)
{
string strSSOLogonFormURL = SingleSignonLocator.GetCredentialEntryUrl("GoogleMail");
Response.Write(User.Identity.Name.ToString() +
"您好!<br>" + "SSO TO GMail Fail!<BR><a href=" + strSSOLogonFormURL +
" target='_self' >找不到對應的帳號,請點此連結輸入Gmail帳號及密碼!</a><P><input type='button' onclick='javascript:window.close()' value='關閉' />");
}
}
catch (SingleSignonException ex)
{
Response.Write(ex.LastErrorCode);
Response.Write(ex.Message);
}
catch (Exception exx)
{
Response.Write(exx.Message);
Response.Write(exx.InnerException);
}
finally
{
if (IntPtr.Zero != pUserName)
{
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(pUserName);
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(pPassword);
}
}
}
}
}
注解:
IntPtr pUserName = IntPtr.Zero;
IntPtr pPassword = IntPtr.Zero;
※拿來接帳號跟密碼的
ISsoProvider isso = SsoProviderFactory.GetSsoProvider();
SsoCredentials myCreds = isso.GetCredentials("GoogleMail");
※取得已定義的 SsoCredentials
pUserName = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(myCreds.UserName);
String userName = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pUserName);
pPassword = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(myCreds.Password);
String Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pPassword);
※轉成看得懂的帳號跟密碼
catch (SingleSignonCredsNotFoundException ssoe)
{
if (SSOReturnCodes.SSO_E_CREDS_NOT_FOUND == ssoe.LastErrorCode)
{
※Credentials could not be found 當發生找不到Current User 的Credentials 時發生的錯誤
string strSSOLogonFormURL = SingleSignonLocator.GetCredentialEntryUrl("GoogleMail");
※取得GoogleMail 這個EAP輸入使用者Credential資料的Url
Response.Write(User.Identity.Name.ToString() +
"您好!<br>" + "SSO TO GMail Fail!<BR><a href=" + strSSOLogonFormURL +
" target='_self' >找不到對應的帳號,請點此連結輸入Gmail帳號及密碼!</a><P><input type='button' onclick='javascript:window.close()' value='關閉' />");
}
}
※string strHTML =….略
別問我這段為何這樣做,我是去打開Gmail登入頁面抄來組的,請依您的情況做調整
這樣SSO部份就完成了,很簡單吧。
之後我建了一個頁面SSODEMO.aspx,只是為了彈出剛剛做的頁面。
以上...完成嚕!
以上簡單的介紹希望對大家有幫助,也歡迎大家跟我討論,或是分享您的應用給我。