问题
I'm running into a problem on our test environment, which has a limit of two users, where it constantly boots me out because the Web API user logs in over and over as needed to sync data. So I thought I'd ask to see if there is a way to get the Web API to log out when it finishes. Is this possible?
回答1:
What version of acumatica?
Acumatica 4.x and below did not have a method to log off. What I've done is create a singleton class for my Acumatica methods and add a common "Validate" method so it re-uses an existing connection.
Something like the following:
private void Validate(bool forceLogin = false, int trycount = 0)
{
if (trycount == 2)
{
//throw new AcumaticaException("Could not login to Acumatica Instance");
}
if (_container == null || forceLogin)
{
_container = new System.Net.CookieContainer();
if (forceLogin != true)
{
forceLogin = true;
}
}
_guruscreen = new Screen
{
CookieContainer = _container,
AllowAutoRedirect = true,
EnableDecompression = true,
Timeout = 1000000,
Url = AcumaticaUrl
};
if (forceLogin)
{
_guruscreen.Login("webservice", "WebServicePwd");
}
try
{
_guruscreen.UntypedClear("CW900000");
}
catch
{
// if session expired, login again and resend request
Validate(true, trycount++);
}
}
Then in each of my methods before calling the Export/Submit I call Validate() to see if an existing connection exists, see if it's still active and if not try to reconnect.
If you are in Acumatica 5.x, there is a logout method.
A sample of that is below
private const string Url = "http://localhost/WebServices_5-0/Soap/DemoService.asmx";
private const string UntypedUrl = "http://localhost/WebServices_5-0/Soap/.asmx";
private const string Login = "xxxxx";
private const string Password = "XXX";
public void ExportStockItems()
{
Screen context = new Screen();
context.CookieContainer = new System.Net.CookieContainer();
context.Url = Url;
context.Login(Login, Password);
IN202500Content stockItemsSchema = context.IN202500GetSchema();
var commands = new Command[]
{
stockItemsSchema.StockItemSummary.ServiceCommands.EveryInventoryID,
stockItemsSchema.StockItemSummary.InventoryID,
stockItemsSchema.StockItemSummary.Description,
stockItemsSchema.GeneralSettingsItemDefaults.ItemClass,
stockItemsSchema.GeneralSettingsUnitOfMeasureBaseUnit.BaseUnit,
stockItemsSchema.PackagingDimensions.Volume,
stockItemsSchema.PackagingDimensions.Weight
};
var filters = new Filter[]
{
new Filter
{
Field = new Field
{
ObjectName = stockItemsSchema.StockItemSummary.InventoryID.ObjectName,
FieldName = "LastModifiedDateTime"
},
Condition = FilterCondition.Less,
Value = DateTime.Now.ToLongDateString(),
Operator = FilterOperator.And
},
new Filter
{
Field = new Field
{
ObjectName = stockItemsSchema.StockItemSummary.ItemStatus.ObjectName,
FieldName = stockItemsSchema.StockItemSummary.ItemStatus.FieldName
},
Condition = FilterCondition.Equals,
Value = "Active",
}
};
var items = context.IN202500Export(commands, filters, 0, false, false);
UntypedDemoServiceRef.Screen untypedContext = new UntypedDemoServiceRef.Screen();
context.Url = UntypedUrl;
untypedContext.CookieContainer = context.CookieContainer;
untypedContext.Logout();
}
来源:https://stackoverflow.com/questions/31441694/how-do-i-log-out-the-acumatica-web-api-user