Example #1
0
    private static async Task <string> GetXTokenAsync()
    {
        string token = string.Empty;

        // Get an XToken
        // Find the account provider using the signed in user.
        // We always use the 1st signed in user, because we just need a valid token. It doesn't
        // matter who's it is.
        Windows.System.User currentUser;
        WebTokenRequest     request;
        var users = await Windows.System.User.FindAllAsync();

        currentUser = users[0];
        WebAccountProvider xboxProvider = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://xsts.auth.xboxlive.com", "", currentUser);

        // Build the web token request using the account provider.
        // Url = URL of the service we are getting a token for - for example https://apis.mycompany.com/something.
        // As this is a sample just use xboxlive.com
        // Target & Policy should always be set to "xboxlive.signin" and "DELEGATION"
        // For this call to succeed your console needs to be in the XDKS.1 sandbox
        request = new Windows.Security.Authentication.Web.Core.WebTokenRequest(xboxProvider);
        request.Properties.Add("Url", "https://xboxlive.com");
        request.Properties.Add("Target", "xboxlive.signin");
        request.Properties.Add("Policy", "DELEGATION");

        // Request a token - correct pattern is to call getTokenSilentlyAsync and if that
        // fails with WebTokenRequestStatus.userInteractionRequired then call requestTokenAsync
        // to get the token and prompt the user if required.
        // getTokenSilentlyAsync can be called on a background thread.
        WebTokenRequestResult tokenResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(request);

        //If we got back a token call our service with that token
        if (tokenResult.ResponseStatus == WebTokenRequestStatus.Success)
        {
            token = tokenResult.ResponseData[0].Token;
        }
        else if (tokenResult.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired)
        { // WebTokenRequestStatus.userInteractionRequired = 3
          // If user interaction is required then call requestTokenAsync instead - this will prompt for user permission if required
          // Note: RequestTokenAsync cannot be called on a background thread.
            WebTokenRequestResult tokenResult2 = await WebAuthenticationCoreManager.RequestTokenAsync(request);

            //If we got back a token call our service with that token
            if (tokenResult2.ResponseStatus == WebTokenRequestStatus.Success)
            {
                token = tokenResult.ResponseData[0].Token;
            }
            else if (tokenResult2.ResponseStatus == WebTokenRequestStatus.UserCancel)
            {
                // No-op
            }
        }
        return(token);
    }
Example #2
0
    private static async Task <string> GetXTokenAsync()
    {
        string token = string.Empty;

        TaskCompletionSource <string> getTokenTaskCompletionSource = new TaskCompletionSource <string>();
        Task <string> getTokenTask = getTokenTaskCompletionSource.Task;

        try
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                // Get an XToken
                // Find the account provider using the signed in user.
                // We always use the 1st signed in user, because we just need a valid token. It doesn't
                // matter who's it is.
                Windows.System.User currentUser;
                WebTokenRequest request;
                var users = await Windows.System.User.FindAllAsync();
                if (users.Count > 0)
                {
                    currentUser = users[0];
                    WebAccountProvider xboxProvider = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://xsts.auth.xboxlive.com", "", currentUser);

                    // Build the web token request using the account provider.
                    // Url = URL of the service we are getting a token for - for example https://apis.mycompany.com/something.
                    // As this is a sample just use xboxlive.com
                    // Target & Policy should always be set to "xboxlive.signin" and "DELEGATION"
                    // For this call to succeed your console needs to be in the XDKS.1 sandbox
                    request = new Windows.Security.Authentication.Web.Core.WebTokenRequest(xboxProvider);
                    request.Properties.Add("Url", "https://mixer.com");
                    request.Properties.Add("Target", "xboxlive.signin");
                    request.Properties.Add("Policy", "DELEGATION");

                    // Request a token - correct pattern is to call getTokenSilentlyAsync and if that
                    // fails with WebTokenRequestStatus.userInteractionRequired then call requestTokenAsync
                    // to get the token and prompt the user if required.
                    // getTokenSilentlyAsync can be called on a background thread.
                    WebTokenRequestResult tokenResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(request);
                    //If we got back a token call our service with that token
                    if (tokenResult.ResponseStatus == WebTokenRequestStatus.Success)
                    {
                        token = tokenResult.ResponseData[0].Token;
                    }
                    else if (tokenResult.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired)
                    { // WebTokenRequestStatus.userInteractionRequired = 3
                      // If user interaction is required then call requestTokenAsync instead - this will prompt for user permission if required
                      // Note: RequestTokenAsync cannot be called on a background thread.
                        WebTokenRequestResult tokenResult2 = await WebAuthenticationCoreManager.RequestTokenAsync(request);
                        //If we got back a token call our service with that token
                        string tokenResultString = tokenResult2.ResponseStatus.ToString();
                        if (tokenResult2.ResponseStatus == WebTokenRequestStatus.Success)
                        {
                            token = tokenResult.ResponseData[0].Token;
                        }
                        else if (tokenResult2.ResponseStatus == WebTokenRequestStatus.UserCancel)
                        {
                            Debug.Log("Error: Unable to get an XToken, the user denied this app access to get an XToken.");
                        }
                        else if (tokenResult2.ResponseStatus == WebTokenRequestStatus.ProviderError)
                        {
                            Debug.Log("Error: Unable to get an XToken, please check the IDs for this game.");
                        }
                    }
                    else if (tokenResult.ResponseStatus == WebTokenRequestStatus.ProviderError)
                    {
                        Debug.Log("Error: Unable to get an XToken, please check the IDs for this game.");
                    }
                    getTokenTaskCompletionSource.SetResult(token);
                }
                else
                {
                    Debug.Log("Error: No users signed in.");
                }
            });
        }
        catch (Exception ex)
        {
            Debug.Log("Error: Unexpected error retrieving an XToken. Exception details: " + ex.Message);
        }

        token = getTokenTask.Result;
        return(token);
    }
Example #3
0
        public static IAsyncInfo RequestTokenWithWebAccountForWindowAsync(IntPtr appWindow, WebTokenRequest request, WebAccount webAccount)
        {
            var iid = GuidGenerator.CreateIID(typeof(IAsyncOperation <WebTokenRequestResult>));

            return((IAsyncInfo)webAuthenticationCoreManagerInterop.RequestTokenWithWebAccountForWindowAsync(appWindow, request, webAccount, iid));
        }