Linked Questions

Popular Questions

Azure Function Runs Locally But Not On Azure

Asked by At

My function is running locally but when I publish it to Azure it is erroring.

The error is

Value cannot be null. Parameter name: format

Googling this seems to suggest the input to the function is wrong but I am posting the exact same JSON that allows it to run locally.

I am lost to how I fix this. Any ideas?

Code below

using System;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;



namespace MyFunction
{
    public static class Login
    {
        [FunctionName("Login")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
        {
            Boolean websiteEnabled = false;
            Guid contactId = new Guid();
            log.LogInformation("C# HTTP trigger function processed a request.");
            dynamic data = await req.Content.ReadAsAsync<object>();
            string username = data?.username;
            string password = data?.password;
            string passwordHash = "";
            User user = new User();
            OrganizationServiceProxy _serviceProxy;
            IOrganizationService _service;
            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = ConfigurationManager.AppSettings["OrganisationUsername"];
            clientCredentials.UserName.Password = ConfigurationManager.AppSettings["OrganisationPassword"];
            Uri organisationUri = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));
            Uri realm = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));
            using (_serviceProxy = new OrganizationServiceProxy(organisationUri, realm, clientCredentials, null))
            {
                _serviceProxy.EnableProxyTypes();
                _service = (IOrganizationService)_serviceProxy;
                QueryByAttribute querybyattribute = new QueryByAttribute("contact");
                querybyattribute.ColumnSet = new ColumnSet("cbob_websitepassword","cbob_websiteenabled","contactid","fullname", "parentcustomerid");
                querybyattribute.Attributes.AddRange("emailaddress1");
                querybyattribute.Values.AddRange(username);
                EntityCollection retrieved = _service.RetrieveMultiple(querybyattribute);
                if(retrieved.Entities.Count == 1)
                {
                    passwordHash = retrieved.Entities[0].GetAttributeValue<String>("cbob_websitepassword");
                    websiteEnabled = retrieved.Entities[0].GetAttributeValue<Boolean>("cbob_websiteenabled");
                    contactId = retrieved.Entities[0].GetAttributeValue<Guid>("contactid");

                    user.Account = retrieved.Entities[0].GetAttributeValue<EntityReference>("parentcustomerid").Name.ToString();
                    user.Email = username;
                    user.LoggedInUser = retrieved.Entities[0].GetAttributeValue<String>("fullname");
                    user.AccountID = retrieved.Entities[0].GetAttributeValue<EntityReference>("parentcustomerid").Id.ToString();
                    user.BookingID = retrieved.Entities[0].Id.ToString();
                } else
                {
                    return req.CreateResponse(HttpStatusCode.BadRequest, "Not allowed");
                }


            }



            Boolean hash = bCryptHash(passwordHash, contactId.ToString() +  "-" + password);
            Console.WriteLine(hash);

            if (!websiteEnabled)
            {
                return req.CreateResponse(HttpStatusCode.BadRequest, "Not allowed");
            }

            if (hash)
            {
                string output = JsonConvert.SerializeObject(user).ToString();
                return req.CreateResponse(HttpStatusCode.OK, output);
            } else
            {
                return req.CreateResponse(HttpStatusCode.BadRequest, "Not allowed");
            }


        }

        public static Boolean bCryptHash(string hash, string submitted)
        {
            Boolean hashPassword = BCrypt.Net.BCrypt.Verify(submitted,hash);
            return hashPassword;
        }

        public static String sha256_hash(string value)
        {
            StringBuilder Sb = new StringBuilder();

            using (var hash = SHA256.Create())
            {
                Encoding enc = Encoding.UTF8;
                Byte[] result = hash.ComputeHash(enc.GetBytes(value));

                foreach (Byte b in result)
                    Sb.Append(b.ToString("x2"));
            }

            return Sb.ToString();
        }
    }
}

Related Questions