Bot Answers even after Logout
Github issues for C# /JS / Java/ Python should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.
Sample information
https://github.com/microsoft/BotBuilder-Samples/tree/main/experimental/teams-sso/csharp_dotnetcore
- Sample type: BotBuilder-Samples/experimental/teams-sso/csharp_dotnetcore/
- Sample language: dotnetcore
- Sample name: csharp_dotnetcore
Describe the bug
Give a clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
- Login
- It opens the It opens Oauth Permissions
- Enter/Provide the permission (Continue)
- Login successful
- Search for any text after overriding the OnMessageActivityAsync method
- Bot will answer
- Type logout
- Again search for anything Bot will answer first
Expected behavior
After logout Bot should not answer any questions
Screenshots


Additional context
I have opened the Microsoft ticket -2104200010010965 and I am available to connect during CST time.
@Ravi-Macha, you suggest you are overriding the OnMessageActivityAsync() method. Can you provide that code? I suspect the problem is there.
Testing the sample as-is results in it functioning as expected - I can login, communicate with the bot by viewing my token, and log out. After logging out, the bot requests me to sign in again before allowing me to do anything else.
@stevkan @ryanlengel Sure, I am just calling some sample method from OnMessageActivityAsync(). Even I did not find the way to check the user token present in session context(turnContext) after Logout.
When user enters "logout" In logoutDialog class we are removing user details by await botAdapter.SignOutUserAsync(innerDc.Context, ConnectionName, null, cancellationToken);
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
_logger.LogInformation("Running dialog with Message Activity.");
await ProcessUserMessageAsync(turnContext, cancellationToken);
await _dialogManager.OnTurnAsync(turnContext, cancellationToken);
}
private async Task ProcessUserMessageAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken){
var userState = _userState.GetCachedState(turnContext);
var text = turnContext.Activity.Text.ToLowerInvariant();
_logger.LogInformation($"ProcessUserMessageAsync method USER-TEXT =============== : {text}.");
var tenantId = (string)turnContext.Activity.Conversation.TenantId;
_logger.LogInformation($"ProcessUserMessageAsync method TENANT ID =============== : {tenantId}.");
if (tenantId != null)
{
_logger.LogInformation("================== TENANT ID NOT NULL ============== ");
if(text.IndexOf("ecc") >=0){
await turnContext.SendActivityAsync(MessageFactory.Text($"Here is your Abc Page Url https://abc.com"), cancellationToken);
}else if(text.IndexOf("azure") >=0){
await turnContext.SendActivityAsync(MessageFactory.Text($"Here is your Azure Page Url https://azure.com"), cancellationToken);
}else if(text.IndexOf("iam") >=0){
await turnContext.SendActivityAsync(MessageFactory.Text($"Here is your Iam Page Url https://iam.com"), cancellationToken);
}
}
//await _dialogManager.OnTurnAsync(turnContext, cancellationToken);
//await turnContext.SendActivityAsync(MessageFactory.Text($"No Results Found .. Sorry"), cancellationToken);
}
@Ravi-Macha mentioned me in my PR #3035 so i'm trying to help if i can. There is a lot going on here to get this sample working and i understand the frustration when trying to get it running for the first time. I was I struggled to get this sample working at first myself. The assignee @stevkan is super knowledgeable and an awesome person, but is probably working on a million things right now.
I tested and ran the sample successfully. So unfortunately, I have nothing useful to add in regards to a fix.
I would check your configurations of your Auth application in Azure and double check your keys, callbacks urls, and secrets everywhere they are used. Some useful resources might be this doc and This walkthrough although I am pretty sure you have already seen them, sharing just in case. Another thing I would try is Starting with just the sample and no additional code and see if that works for you. If it does start adding in your customized code one thing at a time to see where it breaks. If it doesn't work there is most likely an issue with the configurations on your AAD app in Azure, or some other configuration.
If you are able to and willing to share your code, I can take a look when I have time as well. Just put it in a private repo and add me as a collaborator, or if you wish i can make a private repo for you and give you access and permission to delete it if that is easier for you.
@JasonSowers @stevkan My code is same as - code I have not modified anything other than adding one extra personal method. I have mentioned in above post. Thank you for looking in to this issue.
Hi @Ravi-Macha, thank you for your patience. Testing the sample in question continues to work as expected. The bot allows me to sign in, view my token, and sign out. Without being signed in, it requests me to do so. When replacing OnMessageActivityAsync() with your code, the bot also still works as expected.
Are you able to post how you setup your OAuth (without including any pertinent details like keys, secrets, etc.)? I see in your last image that you are being prompted to grant permissions, as well.
@stevkan I have just fallowed the steps provided here - Steps Configure.
Also I have attached the Image which I have configured for Bot channel Registration. Let me know if you need steps of AAD configuration also.

You shouldn't need to populate the Token Exchange URL field. I don't know that it is interfering at all as I don't have a grasp on all the intricacies of AAD v2 OAuth. But, I don't have a value there nor do I remember the docs (at the time I created my OAuth connection) specifying adding a value.
Regarding the scopes, I have 'User.Read' and 'User.ReadBasic.All' listed in addition to the ones you show. It's possible that one or both of these are blocking the bot.
Lastly, for Tenant ID, common should work, but you may want to read up on the differences between providing that value vs providing the tenant id. In my case, I have entered the primary domain (found in your AAD overview) which is acceptable.
To sum up, I have:
- Token Exchange URL: blank
- Tenant ID: my primary domain (someDomain.com)
- Scopes: openId profile User.Read User.ReadBasic.All
Try those changes and see if your bot responds any differently when run.
@stevkan Thank you for your reply, I am not sure why your suggestions are different than Microsoft PG team. I have made your suggested changes
- Removed URL to blank
- primary domain not allowed me sign in, hence I reverted to 'common'.
- Scopes as you suggested.
Instead of seeing SSo sign in I started seeing Oauth Sign in. Even my bot answers after SIGNOUT. Please do find screen shots


It looks like your scopes still aren't correct. Can you add all the scopes I listed (plus any others you wish) and try one more time. If it still doesn't work, I'd be happy to jump on a call with you to compare setups.
@stevkan - Yes I do have mentioned configuration, I also observed that I don’t see and cannot set those values through ARM template I Guess. Please do setup a working session with our team.
User.Read User.ReadBasic.All openid profile
Hi @Ravi-Macha - So, I believe we have been we have been searching for a solution on the OAuth part for what is actually a non-issue. I think you are safe using either the PG team's or our method for setting up OAuth as I don't believe the bot responding after logout is tied to it.
I believe it is a non-issue because login state isn't part of the context passed into the OnMessageActivityAsync() activity handler. Additionally, the activity handler is designed to perform whatever logic you have stored within it on every message unless you tell it otherwise.
So, in your use case, the bot will always respond to one of the optional commands even after logout because that is the default process. If you are set on the code you have in OnMessageActivityAsync() living there, then you will need to create some sort of global variable to track user login state so it knows whether to run or not. Otherwise, and in my opinion, the code should be moved to a dialog or step that is called only once the user has been authenticated.
@Ravi-Macha @stevkan sorry for bailing for a bit, been busy trying to keep up with botbuilder at my newest role wish me luck there 🍾 :shipit:
try the scopes in this order specifically, if they are not already:
openId => profile => User.Read => User.ReadBasic.All
you had posted they are in this order:
User.Read User.ReadBasic.All openid profile
For some reason, the order of scopes mattered if I remember correctly from the OAuth card release back when I was on the team, o know it's silly, but give it a shot please, I vaguely remember customers having trouble with this on the Oauth card as well. I'll look if I can dig it up at all this weekend. not sure if this is actually true and will help but it's worth a shot.
@JasonSowers and @stevkan In AD Application registrations we have configured as only "User.read", is that necessary to configure all ? How does this matter?, I have noticed that Bot behaves same when I exchange the Order.
Only openid OR profile - configuration letting me to show logged in token
In AD registration we have configured as Authentication Blade -
Platform web and redirect URL as - https://token.botframework.com/.auth/web/redirect Access tokens (used for implicit flows) - Marked Checked
API Permissions -

@mrivera-ms Can you assist in validating this issue ?
@Ravi-Macha, I confirmed with one of the developers that this is not an issue with the OAuth prompt. When asked if OnMessageActivity will always fire whether the user is logged in or not, this was his response:
if there is no code executing prior to this relying on auth, then yeah: the message would go out.
what i mean is: it is possible to override OnTurnAsync, and do things in such a way that .Base is NOT called all the time, in which case the OnMessageActivity would not fire
but, by and large, the answer is most likely 'yes'
In other words, as I mentioned previously, this is a matter of tracking the user's login state in such a way that the activity handler can respond to it whether that is a global variable, state property, logged status in a db, etc.