Azure函数在本地运行,但不在Azure上运行

时间:2019-02-03 10:18:53

标签: c# .net azure azure-functions

我的函数在本地运行,但是当我将其发布到Azure时却出错了。

错误是

  

值不能为null。参数名称:格式

谷歌搜索这似乎表明该函数的输入是错误的,但是我发布了完全相同的JSON,以允许其在本地运行。

我不知道如何解决此问题。有什么想法吗?

下面的代码

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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

Uri organisationUri = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));
Uri realm = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));

我的猜测是这些行之一或全部可能是问题所在。您在此处使用String.Format,其中第一个参数是format参数。你提供该参数的AppSettings的似乎是不可用的。部署功能时,请确保具有这些配置值。

另外:如果您没有为插入字符串中的String.Format提供任何对象,那么为什么要使用它呢?

答案 1 :(得分:0)

确保已将这些本地应用程序设置(即OrganisationUsername文件中的local.settings.json等添加到“应用程序设置”中)。在Azure门户的“平台功能”>“应用程序设置”中找到它。当我们将Function项目发布到Azure时,是设计使local.settings.json中的内容没有发布,因为它是为本地开发人员设计的。

当我们使用VS发布函数时,会有friendly dialog用于更新应用程序设置。

enter image description here