Example #1
0
        // Gets an access token. First tries to get the access token from the token cache.
        // Using password (secret) to authenticate. Production apps should use a certificate.
        public async Task <string> GetUserAccessTokenAsync(string userId)
        {
            var account = await _app.GetAccountAsync(userId);

            if (account == null)
            {
                throw new ServiceException(new Error
                {
                    Code    = "TokenNotFound",
                    Message = "User not found in token cache. Maybe the server was restarted."
                });
            }

            try
            {
                var result = await _app.AcquireTokenSilent(_scopes, account).ExecuteAsync();

                return(result.AccessToken);
            }

            // Unable to retrieve the access token silently.
            catch (Exception ex)
            {
                var errorInfo = new Models.ErrorInfo()
                {
                    StatusCode = (int)GraphErrorCode.AuthenticationFailure,
                    Message    = "Caller needs to authenticate. Unable to retrieve the access token silently. " + ex.Message
                };
                throw new ServiceException(errorInfo.AsGraphError());
            }
        }
Example #2
0
        public static Task ErrorOutputResponse(HttpContext httpContext, Models.ErrorInfo result)
        {
            httpContext.Response.ContentType = "application/json";

            var json = new JObject(
                new JProperty("status", result.StatusCode),
                new JProperty("results", result.ToString())
                );

            return(httpContext.Response.WriteAsync(json.ToString(Formatting.Indented)));
        }