ログインをおこなった後に、クライアントアプリケーションがIMAPSサーバ以外のサーバにアクセスする場合に、認証情報を受け渡す事ができます。この機能はIMAPSサーバの認証機構と業務サーバとの間でSSO的な振る舞いをおこないたい場合に便利です。SSO的な振る舞いとは以下のような動作を指します。
このような振る舞いを実現するためには、以下のようにします。
import java.net.Proxy; ・ ・ private void connection(Context context, String url) { new AsyncConnectTask(context, url).execute(); } class AsyncConnectTask extends AsyncTask <String, Integer, String> { LoginManager mLoginManager = null; String mURL = null; ・・・・ public AsyncConnectTask(Context context, String url) { mLoginManager = new LoginManager(context); mURL = url; ・・・・ } protected String doInBackground(String... params) { try { URL url = new URL(mURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); ・・・ mLoginManager.setRequestAuth(con); con.connect(); mLoginManager.checkServerTimeout(con); ・・・ mLoginManager.saveResponseAuth(con); ・・・ } catch (IMAPSAuthTimeOutException e) { // ログアウト処理の実装を行うことを推奨します。 } catch (例外キャッチ) { // キャッチした例外の内容に応じて、例外処理を実装します。 } }
- (void)func { NSURL *url = [NSURL URLWithString:接続先URL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; IMALoginManager *loginManager = [[IMALoginManager alloc] init]; NSError *anError = nil; BOOL result = [loginManager setRequestAuth:request error:&anError]; if(result == NO) { // それぞれのエラーの実装. } [NSURLConnection connectionWithRequest:request delegate:self]; [loginManager setRequestAuth:request error:&anError]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { IMALoginManager *loginManager = [[IMALoginManager alloc] init]; NSError *anError = nil; BOOL result = [loginManager checkServerTimeout:response error:&error]; if(result == NO) { // それぞれのエラーの実装. } else { result = [loginManager saveResponseAuth:response error:&error]; if(result == NO) { // それぞれのエラーの実装. } } }
using Windows.Web.Http; using Windows.Web.Http.Filters; ・ ・ private async void connectionFunc() { string url = "https://サーバアドレス:ポート"; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)); try { LoginManager lm = new LoginManager(); lm.setRequestAuth(request); HttpBaseProtocolFilter hbpf = new HttpBaseProtocolFilter(); hbpf.AllowUI = false; // ユーザー資格情報の入力を求めるプロンプトを表示しない. HttpClient httpClient = new HttpClient(hbpf); HttpResponseMessage response = await httpClient.SendRequestAsync(request); lm.checkServerTimeout(response); } catch (IMAPSAuthTimeOutException e) { // ログアウト処理の実装を行うことを推奨します。 } catch (例外キャッチ) { // キャッチした例外の内容に応じて、例外処理を実装します。 } }