//NOTE: Used by SpsSsoProvider, necessary for SimpleSSOProvider when used by Excel Services
//TODO: Implement Ticket management code; currently just return a string return \; }
public void PutIdentityOnRequest(ref
System.Web.Services.Protocols.HttpWebClientProtocol request, string AppID) {
//NOTE: Used by SpsSsoProvider, not necessary for SimpleSSOProvider throw new NotSupportedException(); }
public void PutIdentityOnRequestUsingTicket(ref
System.Web.Services.Protocols.HttpWebClientProtocol request, string Ticket, string AppID) {
//NOTE: Used by SpsSsoProvider, not necessary for SimpleSSOProvider throw new NotSupportedException(); } } }
至少必须实现 GetCredentials 和 GetSsoProviderInfo 方法。所创建的 SimpleSSOProvider 类根据当前用户以及所提供的应用程序标识符 (AppID) 返回新的凭据。可以
使
用
正
在
执
行
的
线
程
的
CurrentPrincipal
属
性
(System.Threading.Thread.CurrentPrincipal) 获取有关当前用户的信息。以下代码介绍GetCredentials 方法的实现。 C#
public SsoCredentials GetCredentials(string AppID) {
//NOTE: Used by SpsSsoProvider, necessary for any SimpleSSOProvider System.Diagnostics.Trace.WriteLine(\SimpleSSOProvider::GetCredentials\); System.Diagnostics.Trace.Indent();
// Retrieve the logged in user's information string domain = System.Environment.UserDomainName;
System.Diagnostics.Trace.WriteLine(\ + domain); try {
System.Diagnostics.Trace.WriteLine(\ + System.Threading.Thread.CurrentPrincipal.Identity.Name);
// Start building an SsoCredentials object to store two values - UserName and Password
SsoCredentials creds = new SsoCredentials();
creds.Evidence = new System.Security.SecureString[2];
switch (AppID){
case \:
System.Diagnostics.Trace.WriteLine(\AdventureWorks\);
if
(System.Threading.Thread.CurrentPrincipal.IsInRole(\)) {
System.Diagnostics.Trace.WriteLine(\is in InternalSales? \ + System.Threading.Thread.CurrentPrincipal.IsInRole(\)); // Provide components for the InternalAccess account token creds.Evidence[0] = MakeSecureString(domain + \);
creds.Evidence[1] = MakeSecureString(\); } else {
// Provide components for the ExternalAccess account token creds.Evidence[0] = MakeSecureString(domain + \);
creds.Evidence[1] = MakeSecureString(\); } break;
default: throw new
SingleSignonException(SSOReturnCodes.SSO_E_APPLICATION_NOT_FOUND); }
// Put the UserName/Password values into the credential object creds.UserName = creds.Evidence[0]; creds.Password = creds.Evidence[1];
System.Diagnostics.Trace.Unindent(); return creds; }
catch(SingleSignonException ex) {
System.Diagnostics.EventLog.WriteEntry(\, \SSO Exception: \ + ex.ToString()); throw; }
catch(Exception ex) {
System.Diagnostics.EventLog.WriteEntry(\, \Exception: \ + ex.ToString());
throw new SingleSignonException(SSOReturnCodes.SSO_E_EXCEPTION, ex); } }
SsoProvider 实现不需要 SsoCredentialsContents,但某些其他客户端应用程序可能需要 SsoCredentialsContents。在所提供的示例中,Excel Services 将尝试通过 Windows 登录使用已设置的 UserName 和 Password 连接到资源。如果尚未提供 WindowsCredentials 的值,则将在连接字符串中设置 UserName和 Password。 名称 说明 无 UserName 未提供证据。 UserName 存在时设置。 Password Evidence MappedGroup WindowsCredentials
Password 存在时设置。
使用扩展字段(合计最多五个,包括 UserName 和 Password)时设置。应用程序定义为 Group 定义时设置。 应用程序定义为 Windows 身份验证时设置。
GetSsoProviderInfo 方法仅返回有关提供程序的信息,如 Vendor 名称和 Version,如下面的代码所示。 C#
public SsoProviderInfo GetSsoProviderInfo() {
//NOTE: Used by SpsSsoProvider, necessary for any SimpleSSOProvider SsoProviderInfo ssoProvInfo = new SsoProviderInfo();
ssoProvInfo.AssemblyName = Assembly.GetExecutingAssembly().FullName; ssoProvInfo.Vendor = \; ssoProvInfo.Version = \;
return ssoProvInfo; } 如
果
Excel
Services
将
使
用
SSO
提
供
程
序
,
则
还
必
须
提
供 GetCredentialsUsingTicket 和 GetTicket 方法的实现。
我们创建的 SimpleSsoProvider 类是 SSO 提供程序的一个非常简单的示例。实际的实现必须从安全库检索凭据,并保护内存中存储的任何值。 GetCredentials
返
回
的
SsoCredentials
对
象
使
用
SecureString
类
存
储 UserName 和 Password 属性以及所有 Evidence 值。SecureString 对其数据进行加密,使这些数据不会被轻易解密。 异常处理
如果我们的 SimpleSSOProvider 无法正确地确定 AppID,则引发 SingleSignonException 的实例,并使用标准 SSOReturnCodes 字段。下表显示了用于几种错误情况的一些常见 SSOReturnCodes 字段。