Teemu Tapanila bio photo

Teemu Tapanila

CTO of Mallow

Email Twitter LinkedIn Github

OAuth is open authentication standard that can be used to give an access to the specific resources of the account. Instead of giving full access in form of username and password to 3rd party applications. Microsoft added a option to use OAuth in WinRT.

Here’s an example how to use it to authenticate to Facebook.

[sourcecode language=”csharp”] private async void Authenticate() { //Facebook Authentication Uri var facebookUri = "https://www.facebook.com/dialog/oauth"; //Standard redirect uri for desktop/non-web based apps var redirectUri = "https://www.facebook.com/connect/login_success.html"; //Place your appclient id here var clientId = ""; //The type of token that can be requested var responseType = "token"; //Response pattern var pattern = string.Format("{0}#access_token={1}&expires_in={2}", redirectUri, "(?.+)", "(?.+)"); //Access scope wanted var scope = "read_stream"; try { String FacebookURL = "https://www.facebook.com/dialog/oauth?" + "client_id=" + clientId + "&redirect_uri=" + Uri.EscapeUriString(redirectUri) + "&scope=" + scope + "&display=touch&response_type=" + responseType;

        System.Uri StartUri = new Uri(FacebookURL);
        System.Uri EndUri = new Uri(redirectUri);

        WebAuthenticationResult WebAuthenticationResult =
                   await WebAuthenticationBroker.AuthenticateAsync(
                         WebAuthenticationOptions.None,
                         StartUri,
                         EndUri);
        if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
        {
            //Authenticated succesfully
            var response = WebAuthenticationResult.ResponseData.ToString();
        }
        else if
        (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
        {
            //Handle HTTP error
        }
        else
        {
            //Authentication failed
        }
    }
    catch (Exception ex)
    {
        //Handle error
    }
} [/sourcecode]