在ASP.NET中调用SaveChanges ODataClient方法时,“CSRF验证失败”

时间:2016-07-20 17:30:18

标签: c# .net odata

我正在尝试实现一个代码,通过我生成的OData客户端的WCF Dataservices来修改实体。我遵循了此链接中实现的相同原则 - https://msdn.microsoft.com/en-us/library/dd756368(v=vs.110).aspx

我在SaveChanges方法调用期间收到"CSRF token validation failed"错误。请参阅以下代码:

            // Define the URI of the public Northwind OData service.
        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

        Uri cuanUri =
            new Uri("https://xxxx.xxxx.com:44300/sap/opu/odata/sap/CUAN_IMPORT_SRV/", 
                UriKind.Absolute);

        // Create a new instance of the typed DataServiceContext.
        DateTime stimestamp = new DateTime();
        DateTime dob = new DateTime(1992,05,02);
        CUAN_IMPORT_SRV_Entities context = new CUAN_IMPORT_SRV_Entities(cuanUri);
        context.SendingRequest2 += SendBaseAuthCredsOnTheRequest;

        Contact newContact = Contact.CreateContact("C160717055735", "SAP_ODATA_IMPORT", stimestamp);
        //Product newProduct = Product.CreateProduct(0, "White Tea - loose", false);
        newContact.CompanyId = "BLUEFIN1";
        newContact.CompanyIdOrigin = "SAP_ODATA_IMPORT";
        newContact.CountryDescription = "Singapore";
        newContact.DateOfBirth = dob;
        newContact.EMailAddress = "j_prado@yahoo.com";
        newContact.EMailOptIn = "Y";
        newContact.FirstName = "Jeffrey1";
        newContact.FunctionDescription = "Consultant";
        newContact.GenderDescription = "Male";
        newContact.LastName = "Prado1";
        newContact.PhoneNumber = "+6596492714";
        newContact.PhoneOptin = "Y";

        Company newCompany = Company.CreateCompany("BLUEFIN1", "SAP_ODATA_IMPORT", stimestamp);
        newCompany.IndustryDescription = "1007";
        newCompany.CompanyName = "BLUEFIN1";

        Interaction newInteraction = Interaction.CreateInteraction("");
        newInteraction.CommunicationMedium = "WEB";
        newInteraction.ContactId = "C160717055735";
        newInteraction.ContactIdOrigin = "SAP_ODATA_IMPORT";
        newInteraction.InteractionType = "WEBSITE_REGISTRATION";
        newInteraction.Timestamp = stimestamp;

        ImportHeader newHeader = ImportHeader.CreateImportHeader("");
        newHeader.Timestamp = stimestamp;
        newHeader.UserName = "ENG";
        newHeader.SourceSystemType = "EXT";
        newHeader.SourceSystemId = "HYBRIS";

        try
        {
            // Add the new entities to the CUAN entity sets.
            context.AddToImportHeaders(newHeader);
            context.AddToContacts(newContact);
            context.AddToCompanies(newCompany);
            context.AddToInteractions(newInteraction);

            // Send the insert to the data service.              
            DataServiceResponse response = context.SaveChanges();

            // Enumerate the returned responses.
            foreach (ChangeOperationResponse change in response) 
            {
                // Get the descriptor for the entity.
                EntityDescriptor descriptor = change.Descriptor as EntityDescriptor;

                if (descriptor != null)
                {
                    Contact addedContact = descriptor.Entity as Contact;

                    if (addedContact != null)
                    {
                        Console.WriteLine("New contact added with ID {0}.",
                            addedContact.CompanyId);
                    }
                }
            }
        }
        catch (DataServiceRequestException ex)
        {
            throw new ApplicationException(
                "An error occurred when saving changes.", ex);
        }

    }

    private static void SendBaseAuthCredsOnTheRequest(object sender,
            SendingRequest2EventArgs e)
    {
        var authHeaderValue = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}"
            , "XXXXX", "XXXXX")));

        e.RequestMessage.SetHeader("Authorization", "Basic " + authHeaderValue); //this is where you pass the creds.
    }

上面的代码在调用时失败:DataServiceResponse response = context.SaveChanges();

0 个答案:

没有答案