Skip to main content

How to Stop an Adaptive Card from Being Submitted Twice in the Microsoft 365 Agents SDK in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

An Adaptive Card stays in the chat after it's been used, and its buttons keep working. A double click, a second try on a slow connection, or someone scrolling back to an old card, and the same submit reaches your agent again. If that submit gives something away or books something, it happens twice. The fix is to give every card an ID and to remember which IDs your agent has already handled.

The Example​

The agent for this post hands out vouchers. Send it voucher, and it answers with a card, A free coffee, on us, with a Claim button. Each claim gets a voucher code. One card should give one voucher, however many times its button is pressed.

The agent is one class in an ASP.NET Core project with the Agents SDK, hosted with AddAgent<VoucherAgent>(), with MemoryStorage registered as its IStorage, and listening on port 3978. It builds its card in code, as in How to Build an Adaptive Card in Code in the Microsoft 365 Agents SDK in C#.

Prerequisites​

  • The Microsoft 365 Agents Playground, to chat with the agent on your own machine. agentsplayground --version should print 0.2.30; if the command isn't found, install it with npm install -g @microsoft/m365agentsplayground.

Step 1: See the Problem​

The first version of the agent hands out a code on every claim:

VoucherAgent.cs (first version, the handler)
private async Task ClaimVoucherAsync(ITurnContext turnContext, ITurnState turnState, object data, CancellationToken cancellationToken)
{
string code = "COFFEE-" + Random.Shared.Next(1000, 10000);

await turnContext.SendActivityAsync($"Here's your voucher: **{code}**", cancellationToken: cancellationToken);
}

Its card's button carries only a verb, "data": { "verb": "claimVoucher" }, and AdaptiveCards.OnActionSubmit("claimVoucher", ClaimVoucherAsync) routes it. Run the agent with dotnet run, then start the Agents Playground in a second terminal:

Terminal 2
agentsplayground -e http://localhost:3978/api/messages -c emulator

Send voucher, select Claim, wait for the code, and select Claim again on the same card:

The Agents Playground chat: voucher, the card A free coffee, on us with a Claim button, and two answers, Here's your voucher: COFFEE-8479 and Here's your voucher: COFFEE-5634

One card, two vouchers. Nothing about the second submit looks different to the agent: same verb, same data. Microsoft's Copilot Studio documentation describes exactly this: by design, Adaptive Cards allow selecting their submit buttons multiple times, and recommends including identifying data in each submit action and checking it when the submit arrives.

Step 2: Give Each Card an ID​

When the agent sends a card, it makes a new ID and puts it in the button's data, next to the verb. Whatever the button sends back carries that ID with it:

VoucherAgent.cs (SendVoucherCardAsync)
private async Task SendVoucherCardAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
string cardId = Guid.NewGuid().ToString("N");

JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "A free coffee, on us", ["size"] = "Medium", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Claim it to get your voucher code.", ["wrap"] = true }
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Claim",
["data"] = new JsonObject { ["verb"] = "claimVoucher", ["cardId"] = cardId }
}
}
};

await turnContext.SendActivityAsync(MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }), cancellationToken);
}

Guid.NewGuid().ToString("N") makes a random ID of 32 letters and digits, different for every card.

Step 3: Remember the IDs You've Handled​

The handler reads the ID, and checks a list of used IDs in conversation state, where everyone in the chat shares it: the card belongs to the conversation, so one claim per card holds whoever presses the button.

VoucherAgent.cs (ClaimVoucherAsync)
private async Task ClaimVoucherAsync(ITurnContext turnContext, ITurnState turnState, object data, CancellationToken cancellationToken)
{
VoucherClaim claim = ProtocolJsonSerializer.ToObject<VoucherClaim>(data);

if (string.IsNullOrEmpty(claim.CardId))
{
await turnContext.SendActivityAsync("This card can't be used. Send **voucher** for a new one.", cancellationToken: cancellationToken);
return;
}

List<string> usedCards = turnState.Conversation.GetValue<List<string>>(UsedCardsKey, () => []);
if (usedCards.Contains(claim.CardId))
{
await turnContext.SendActivityAsync("This voucher has already been claimed.", cancellationToken: cancellationToken);
return;
}

usedCards.Add(claim.CardId);
turnState.Conversation.SetValue(UsedCardsKey, usedCards);

string code = "COFFEE-" + Random.Shared.Next(1000, 10000);
await turnContext.SendActivityAsync($"Here's your voucher: **{code}**", cancellationToken: cancellationToken);
}

ClaimVoucherAsync in a macOS-style editor window: it reads the card ID from the submit, refuses a missing ID, looks the ID up in the usedCards list in conversation state, answers This voucher has already been claimed. when it's there, and otherwise adds it and sends a new code

Two more pieces go with it: a name for the list, private const string UsedCardsKey = "usedCards"; at the top of the class, and a record for what the button sends, public record VoucherClaim(string? CardId);. A submit without an ID, such as one from a card sent before this change, gets a polite answer instead of a voucher.

Step 4: Try It Again​

Restart the agent, send voucher, select Claim twice, then send voucher again and claim the new card:

The chat: the first card's claim gets Here's your voucher: COFFEE-5731, the second press gets This voucher has already been claimed., and a new card's claim gets Here's your voucher: COFFEE-7804

The first press got a voucher, the second press on the same card was refused, and a new card, with a new ID, worked as it should.

Common Mistakes​

MistakeWhat happensDo this instead
Putting the same ID in every cardAfter the first claim, every card is refusedMake a new ID each time you send a card
Keeping the used IDs in a field of the agentThe agent object is new for every message, so the list is always emptyKeep them in state
Keeping them in user state when the card is for the whole chatEach person can use the same card onceUse conversation state for one use per card

Troubleshooting​

SymptomLikely causeFix
Every claim is refusedThe ID is the same on every card, or never changesCreate the ID inside the method that sends the card
Old cards answer This card can't be usedThey were sent before the cards had IDsSend a new card
The list of used IDs keeps growingEvery card adds oneRemove old IDs, or keep them in a store that expires them

FAQ​

What about two presses at exactly the same moment?​

Two submits that arrive together are handled at the same time, in two turns, and both can find the ID unused. The Agents SDK's state storage refuses the second save in that case, as Fixed: EtagException: AgentState 'conversation' could not save key because another turn updated the state first shows, including what to do about it and the one case the check doesn't cover.

Can I use a domain rule instead of a card ID?​

Often, yes, and it's better when you have one: one desk per person per day, one approval per request. Look for what was already done before doing it again. A card ID is the general answer when there's no such rule.

Can I change the card so the button can't be pressed again?​

Updating a card after it's been sent depends on the channel and the kind of action, and Part 9 of the Microsoft 365 Agents SDK Fundamentals, Universal Actions: Adaptive Cards That Update, will cover it. The check in your handler is needed either way, because a client that keeps old cards clickable can always send a submit again.

Conclusion​

Adaptive Card buttons can be used more than once, so treat every submit as something that may have arrived before. Put an ID of your own in each card's button data, keep the IDs your agent has handled in conversation state, and answer a repeat with a message instead of doing the work again.

Additional Resources​