Error:
—> System.Security.SecurityException: User not authorized to create object type AvontusUser at Avontus.Rental.Wrapper.VendorService.SetBusinessPartner(String XML) — End of inner exception stack trace —
Utilized both Administrator and Corporate Manager profiles that have user creation rights.
The same code on previous versions of Avontus worked OK.
Odd chunk of code there. What is it that you’re trying to accomplish by setting quantities on the last name? If you need a bit of a storage container, you can store xml in attachments somewhere.
Anyhow, this is likely caused by not logging in or wrong credentials when you log in, before you try and add a user. When you log in, make sure you’re grabbing the boolean that confirms that you’ve authenticated properly. Also be sure to make a call to Logout() on the principal object before you load the new one.
// Logout to clear cache
AvontusPrincipal.Logout();
AvontusPrincipal.LoadPrincipal("username");
// Log in
bool success = AvontusPrincipal.Login("username", "yourpassword");
// Write or fail depending on if logged in
if (success)
Console.WriteLine("Login successful");
else
{
Console.WriteLine("Login failed");
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
return;
}
If that still doesn’t work can you send a bit more code in to troubleshoot?
Are you trying to create a user (seen in the users tab) or a contact (related to and seen in the list of contacts on a customer or vendor)? I see code that could be used for either here…
We switched from using the “Corporate Manager” profile (which had user creation perimissions) to the Administrator profile.
We were still seeing issues until we manually logged in with the account using the Avontus UI and now we are able to create contacts for vendors and customers.
Yes, for this you’ll want to create a specific type of user. Sample code below for a customer:
BusinessPartner customer = BusinessPartner.NewBusinessPartner(PartnerTypes.Customer); customer.Name = “My New Customer”;
customer.Save();
AvontusUser user = AvontusUser.NewUser(UserTypes.Customer);
user.RelatedID = customer.BusinessPartnerID;
user.FirstName = "First Name";
user.LastName = "Last Name";
// Get/set the role for the customer. Note that these
// are enum IDs that may match to a different name
RoleList roles = RoleList.GetRoles(false, true);
user.RoleID = roles.GetRoleByType(AvontusRoleType.Customer).RoleID;
// Assign random part of guid so that the username is unique
// If you assign a specific username you can trap an exception
// during save and detect if the unique rule is broken. Let
// me know if you'd like code to see how this works.
user.Username = Guid.NewGuid().ToString().Substring(0, 5);
user.Password = Guid.NewGuid().ToString().Substring(0, 5);
if (user.IsSavable)
user.Save();
else
{
foreach (Avontus.Core.Validation.BrokenRule rul in user.BrokenRulesCollection)
{
Console.WriteLine(rul.Description);
}
}