Note: For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.

C# custom resource-request implementation sample

This sample demonstrates how to get data from a protected resource by using a HttpWebRequest object and the MobileFirst WLAuthorizationManager API for Windows 8 Universal app.

After a response was first received, the WLAuthorizationManager class determines whether this response is a MobileFirst protocol response. If so, the user gets the scope, obtains the authorization header for this scope, and requests the protected resource one more time.

class WebReqInfo {
  public HttpWebRequest request = null;
}
	
private async void externalOAuthFlow() {
  // Create a HttpWebrequest object to the desired URL.
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:3000/v1/apps/1234/test");
  request.BeginGetResponse(new AsyncCallback(responseCallback), new WebReqInfo(){ request=request });
}

private void responseCallback(IAsyncResult asyncResult) { 
  WebReqInfo wReqInfo = null;
  HttpWebResponse response = null;
  try {
    /* Send the request to the server and wait for it to complete */
    wReqInfo = (WebReqInfo)asyncResult.AsyncState;
			
    // Get response
    response = wReqInfo.request.EndGetResponse(asyncResult) as HttpWebResponse;
		
    WLResponse newResponse = new WLResponse(response);
    int sCode = newResponse.getStatus().GetHashCode();

    // Check that the response conforms to the MFP protocol
    if (WLAuthorizationManager.getInstance().isAuthorizationRequired(response)) {
      // Get required scope from response
      String scope = WLAuthorizationManager.getInstance().getAuthorizationScope(response);
    
	  //Obtain authorization header for the scope
      WLAuthorizationManager.getInstance().obtainAuthorizationHeader(scope, new MyResponseListener());
    }	
    else {
      // At this point a response from the resource was received, process it:
      processResponse(response)	
    }
  } catch (Exception e) {
    Debug.WriteLine("Exception during request");
  }
}

public class MyResponseListener : WLResponseListener {
  HttpWebRequest request;
  public MyResponseListener(HttpWebRequest request) {
    this.request = request;
  }
  class WebReqInfo {
    public HttpWebRequest request = null;
  }

  // This method will be invoked if an authorization header was obtained successfully:
  public void onSuccess(WLResponse response) {
  
    // Add the obtained authorization header to the original request
    WLAuthorizationManager.getInstance().addCachedAuthorizationHeader(request);
    try {
      // Resend the request
      request.BeginGetResponse(new AsyncCallback(resendRequestCallback), new WebReqInfo(){request=request });
    } catch (Exception e) {
      Debug.WriteLine("Unable to obtain token");
    }
  }

  // This method will be invoked if an authorization header was not obtained:
  public void onFailure(WLFailResponse response) {
    Debug.WriteLine("Unable to obtain Token");
  }
  
  private void resendRequestCallback(IAsyncResult asyncResult) {
    WebReqInfo wReqInfo = null;
    HttpWebResponse response = null;
    try {
      /* Send the request to the server and wait for it to complete */
      wReqInfo = (WebReqInfo)asyncResult.AsyncState;
      response = wReqInfo.request.EndGetResponse(asyncResult) as HttpWebResponse;
      processResponse(response);
    } catch (Exception e) {
      Debug.WriteLine("Exception during request");
    }
  }
}
For more information about WLAuthorizationManager, see C# client-side API for Windows 10 Universal Windows Platform and Windows 8 Universal apps.