问题
I'm developing a MS Bot with the v4 framework and I'm trying to set up authentication. The problem is that I can authenticate but if I want to recapture the user authentication token by reprompting the dialog (found here) then I do get a new loginprompt instead of just retrieving the token.
This is what tried:
- using Azure AD v1 and v2
- searching for documentation (but is always deprecated or the same as found at the link)
My code (if you need something else feel free to ask):
// Register the Promt
AddDialog(Prompt(ConnectionName));
// Prompts the user to log in using the OAuth provider specified by the connection name.
// Prompt definition
public static OAuthPrompt Prompt(string connectionName) => new OAuthPrompt(
LoginPromptName,
new OAuthPromptSettings
{
ConnectionName = connectionName,
Text = "Please login",
Title = "Login",
Timeout = 300000, // User has 5 minutes to login
});
private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext context, CancellationToken cancellationToken)
{
var state = await UserProfileAccessor.GetAsync(context.Context);
return await context.BeginDialogAsync(LoginPromptName, cancellationToken: cancellationToken);
}
private async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext context, CancellationToken cancellationToken)
{
var loginState = await UserProfileAccessor.GetAsync(context.Context);
// Get the token from the previous step. Note that we could also have gotten the
// token directly from the prompt itself. There is an example of this in the next method.
var tokenResponse = (TokenResponse)context.Result;
if (tokenResponse != null)
{
* DOES SOMETHING WHEN LOGGED IN*
}
else
{
* DOES SOMETHING WHEN LOGIN FAILED *
}
/* !!
HERE IS THE PROBLEM IN STEAD OF CHECKING IF THE USER
IS ALREADY LOGGED IN AND JUST RETRIEVING THE TOKEN I
GET A NEW LOGIN SCREEN.
!!
*/
var token2Response = await context.BeginDialogAsync(LoginPromptName, null, cancellationToken)
}
So basically I want to retrieve the user token so I can check in different methods and dialogs if the user is logged in.
回答1:
This is due to this bug: OAuthPrompt requires user credentials when message contains guid or 6-digit number, which currently has a Pull Request to fix.
The workaround for now would be to make sure that the bot gets any kind of other user response before ending the dialog. The sample does it here with:
return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to view your token?") }, cancellationToken);
I tested adding this to your code in * DOES SOMETHING WHEN LOGGED IN*
and it worked:
来源:https://stackoverflow.com/questions/55970661/ms-bot-framework-doesnt-remember-authentication