Exemplo n.º 1
0
        public static async Task <HttpResponseMessage> AuthenticateContinue(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestMessage req,
            TraceWriter log)
        {
            string id = req.GetQueryNameValuePairs()
                        .FirstOrDefault(q => string.Compare(q.Key, "id", true) == 0)
                        .Value;

            string provider = req.GetQueryNameValuePairs()
                              .FirstOrDefault(q => string.Compare(q.Key, "provider", true) == 0)
                              .Value;

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            CloudTableClient    client         = storageAccount.CreateCloudTableClient();

            CloudTable table = client.GetTableReference("AuthenticationSessions");
            await table.CreateIfNotExistsAsync();

            TableOperation retrieveOperation = TableOperation.Retrieve <OAuthSessionEntity>(provider, id);

            OAuthSessionEntity updatedSession = null;
            bool complete = false;

            while (!complete)
            {
                Thread.Sleep(1000);
                TableResult result = await table.ExecuteAsync(retrieveOperation);

                updatedSession = result.Result as OAuthSessionEntity;
                complete       = updatedSession.State != "new";
            }

            //Create the response JSON and delete the session from table storage
            string retValJSON = updatedSession.ToJSON(false);

            //TableOperation removeOperation = TableOperation.Delete(updatedSession);
            //await table.ExecuteAsync(removeOperation);

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(
                    retValJSON,
                    Encoding.UTF8,
                    "application/json")
            });
        }
Exemplo n.º 2
0
        public static async Task <HttpResponseMessage> AuthenticateBegin(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req,
            TraceWriter log)
        {
            string provider = req.GetQueryNameValuePairs()
                              .FirstOrDefault(q => string.Compare(q.Key, "provider", true) == 0)
                              .Value;

            string requestData = await req.Content.ReadAsStringAsync();

            JObject requestJSON  = JObject.Parse(requestData);
            string  publicRSAKey = requestJSON["PublicRSAKey"].Value <string>();

            OAuthSessionEntity session = new OAuthSessionEntity(provider)
            {
                PublicRSAKey = publicRSAKey
            };

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            CloudTableClient    client         = storageAccount.CreateCloudTableClient();

            CloudTable table = client.GetTableReference("AuthenticationSessions");
            await table.CreateIfNotExistsAsync();

            TableOperation insertOperation = TableOperation.Insert(session);

            table.Execute(insertOperation);

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(
                    session.ToJSON(true),
                    Encoding.UTF8,
                    "application/json")
            });
        }