ユーザーを自動サインインさせる方法
ユーザーを自動サインインさせるには、IdentityManager および Identity の各オブジェクトのインスタンスを格納する変数を初期化しておく必要があります。クラス コンストラクタでこれを行うことをお勧めします。
次のコードは、サンプル アプリケーションからの抜粋です。
public MainWindow()
{
InitializeComponent();
//Try initializing the global instance of IdentityManager.
try
{
oIDMgr = IdentityManager.CreateInstance("Tailspin Toys;someone@tailspintoys.com;Tailspin Toys Application", "Windows Live ID Client Sample");
}
catch (WLLogOnException wlex)
{
//Check to see if FlowUrl is defined.
if (wlex.FlowUrl != null)
{
//If FlowUrl is defined, direct user to the web page to correct the error.
MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
}
else
{
//If FlowUrl is not defined, simply display the ErrorString.
MessageBox.Show(wlex.ErrorString);
}
}
//Check the config file to see if a default user is defined.
defaultUserName = ConfigurationManager.AppSettings["defaultUserName"];
if (!String.IsNullOrEmpty(defaultUserName))
{
TrySilentSignIn();
}
else
{
//If no default user is defined, try instantiating the global Identity object instance from scratch.
try
{
oID = oIDMgr.CreateIdentity();
}
catch (WLLogOnException wlex)
{
//Check to see if FlowUrl is defined.
if (wlex.FlowUrl != null)
{
//If FlowUrl is defined, direct user to the web page to correct the error.
MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
}
else
{
//If FlowUrl is not defined, simply display the ErrorString.
MessageBox.Show(wlex.ErrorString);
}
}
}
UpdateDisplay();
}
このコードは、MainWindow クラスが作成されるときに実行されます。まず、CreateInstance メソッドが呼び出され、IdentityManager のグローバル インスタンスを格納する変数が初期化されます。
次に、アプリケーション構成ファイルから defaultUserName という名前の変数がロードされます。構成ファイルに情報を保存したり、構成ファイルから情報を取得したりする方法の詳細については、「個人用設定の実装」を参照してください。defaultUserName の値が構成ファイルに設定されている場合には、TrySilentSignIn が呼び出されます。以下は、サンプル アプリケーションから抜粋した TrySilentSignIn メソッドの定義です。
private void TrySilentSignIn()
{
//Try instantiating the global Identity object instance with the username from the config file.
try
{
oID = oIDMgr.CreateIdentity(defaultUserName);
}
catch (WLLogOnException wlex)
{
//Check to see if FlowUrl is defined.
if (wlex.FlowUrl != null)
{
//If FlowUrl is defined, direct user to the web page to correct the error.
MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
}
else
{
//If FlowUrl is not defined, simply display the ErrorString.
MessageBox.Show(wlex.ErrorString);
}
}
//Check that the username is valid.
if (oID != null)
{
//Check to make sure the user has stored their username and password.
if (oID.SavedCredentials == CredentialType.UserNameAndPassword)
{
try
{
//Try silent authentication.
if (oID.Authenticate(AuthenticationType.Silent))
{
currentUserName = defaultUserName;
}
else
{
MessageBox.Show("Default user's stored sign-in name and password are invalid.");
}
}
catch (WLLogOnException wlex)
{
//Check to see if FlowUrl is defined.
if (wlex.FlowUrl != null)
{
//If FlowUrl is defined, direct user to the web page to correct the error.
MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
}
else
{
//If FlowUrl is not defined, simply display the ErrorString.
MessageBox.Show(wlex.ErrorString);
}
}
}
else
{
MessageBox.Show("Default user hasn't stored sign-in name and password.");
}
}
else
{
MessageBox.Show("defaultUserName in config file has an invalid value.");
config.AppSettings.Settings.Remove("defaultUserName");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
まず、保存されているサインイン名を使用して CreateIdentity メソッドが呼び出されます。
返された Identity オブジェクト インスタンスが null の場合、構成ファイルに指定されているサインイン名は無効であることがわかります。その結果、無効な defaultUserName 設定は構成ファイルから削除されます。
返された Identity オブジェクト インスタンスが null でない場合、構成ファイルに指定されているサインイン名が有効であることがわかります。
次に、Identity オブジェクトの SavedCredentials プロパティが検査されて、サインイン名およびパスワードが以前に保存されているかどうかが検証されます。サインイン ダイアログ ボックスでサインイン名とパスワードの両方の保存を選択しており、ユーザー インターフェイスでのサインインに成功したことのあるユーザーだけが、自動サインインの対象となります。
必要な Windows Live ID を以前にユーザーがコンピュータに保存していた場合、Silent パラメータを指定して Authenticate メソッドが呼び出され、サインイン ダイアログ ボックスが表示されることなくユーザーが認証されます。
Authenticate の呼び出しは、try/catch ブロック内に入れ子にされています。これにより、Authenticate の呼び出しによって生成される可能性がある例外をキャッチして処理できます。例外を処理することで、アプリケーションがより堅牢なものになります。
保存されている Windows Live ID が有効な場合、Authenticate の呼び出しの結果 true が返されます。ここで、プログラム制御が TrySilentSignIn からコンストラクタに返されます。続いて、サインインの成功に従って表示が更新され、ユーザーが個人用に設定した機能が表示されます。詳細については、「個人用設定の実装」を参照してください。
関連項目
タスク
自動サインインのセットアップ方法
Windows Live ID サンプル アプリケーションの実行