I am creating a Windows Phone 8.1 RT application. I have installed the facebook and facebook.client sdks for setting up login and sharing purposes. For login purpose I have followed the steps as mentioned here.
My App.xaml.cs OnActivated function looks like this:
protected override void OnActivated(IActivatedEventArgs args)
{
base.OnActivated(args);
var protocolArgs = args as ProtocolActivatedEventArgs;
if (protocolArgs != null)
{
LifecycleHelper.FacebookAuthenticationReceived(protocolArgs);
}
Session.OnFacebookAuthenticationFinished += OnFacebookAuthenticationFinished;
}
and here is OnFacebookAuthenticationFinished method
private async void OnFacebookAuthenticationFinished(AccessTokenData session)
{
await Session.CheckAndExtendTokenIfNeeded();
if (Constant.fbSignup)
{
User fbUser = new User();
Account userAccount = new Account();
try
{
FacebookClient fbClient = new FacebookClient(session.AccessToken);
dynamic result = await fbClient.GetTaskAsync("me?fields=id,first_name,last_name,email,location");
fbUser.FirstName = result.first_name;
fbUser.LastName = result.last_name;
userAccount.UserName = result.email;
fbUser.UserAccount = userAccount;
//fbUser.City = result.location.name;
Constant.User = fbUser;
RootFrame.Navigate(typeof(SignUpPage));
}
catch (Exception ex)
{
await new MessageDialog(ex.Message).ShowAsync();
}
}
The login works fine.
Now I want to share some content using the Session.ShowFeedDialog(). I have followed the steps mentioned here for creating AppRequests within the dialog.
I am calling the ShowFeedDialog method this way from a page StoreDetailsPage.xaml.cs
All the following code rests in StorePageDetails.xaml.cs
Session.ShowFeedDialog("", link, linkDescription, linkCaption);
The posting also works fine. But I need to check whether the post was successful or not. For this purpose I tried the
Session.OnFacebookFeedFinished = Success;
where success is
public delegate void FacebookDelegate(FBResult result);
void Success(FBResult result)
{
//Code to check if post was successful
}
So my problem is after ShowFeedDialog is closed the OnActivated event is called and success delegate method is never reached or not called.
I haven't used delegates before so I don't know if there is something wrong there. Also I haven't figured out what the logic for post verification should since I was not able to step into this function. So any suggestions would be much appreciated
Related
I am developing a desktop application in WPF with C #, which connects to an Azure AD application, when trying to log out of the official documentation, it only deletes the cache but does not log out of the application, so when trying to connect again I already appear as disconnected
This is my code for login, but it doesn't work.
//cerrar sesion y volver a pantalla de menu
public async void cerrarSesion()
{
var accounts = await app2.PublicClientApp.GetAccountsAsync();
if (accounts.Any())
{
try
{
System.Diagnostics.Process.Start("https://login.microsoftonline.com/4fb44f4b-6f0e-46f2-acea-ac4538df0c9c/oauth2/v2.0/logout");
await app2.PublicClientApp.RemoveAsync(accounts.FirstOrDefault()).ConfigureAwait(false);
Console.WriteLine("User has signed-out");
//NavigationService.Navigate(new Menu());
// System.Diagnostics.Process.Start("https://login.microsoftonline.com/4fb44f4b-6f0e-46f2-acea-ac4538df0c9c/oauth2/v2.0/logout");
// Console.WriteLine(Resultado.Account.HomeAccountId + "");
}
catch (MsalException ex)
{
Console.WriteLine($"Error signing-out user: {ex.Message}");
}
}
}
Stay logged in
AFAIK there is no way by default to remove the Session cookies from WPF web browser.You can find the reference from here.
There is already a Github issue raised for the same which can be tracked from here, which currently in a blocked state. Will be prioritized later and worked upon:
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/425
I would suggest you to try to suppress all your cookies for achieving the same.
Please take a look at this link and see if it helps.
Also check below link for reference.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/2871993e-744e-46cf-8adc-63c60018637c/deleting-cookies-in-wpf-web-browser-control?forum=wpf
Alternatively, suggested by Bigdan Gavril, in the below link
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/425
You have some control over the browser by using the .WithPrompt method. AFAIK Prompt.ForceLogin will always force the user to enter their password.
var result = await pca.AcquireTokenInteractive(_scopes)
.WithPrompt(Prompt.ForceLogin)
.ExecuteAsync()
Hope it helps.
According to my test, we can open an embedded web browser to send a logout request. Then it will help us clear cookie and session. When we login again, we need to enter username and password. For example
private async void SignOutButton_Click(object sender, RoutedEventArgs e)
{
var accounts = await App.PublicClientApp.GetAccountsAsync();
if (accounts.Any())
{
try
{
await App.PublicClientApp.RemoveAsync(accounts.FirstOrDefault());
NavigationWindow window = new NavigationWindow();
window.Source = new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/logout");
window.Show();
this.ResultText.Text = "User has signed-out";
this.CallGraphButton.Visibility = Visibility.Visible;
this.SignOutButton.Visibility = Visibility.Collapsed;
}
catch (MsalException ex)
{
ResultText.Text = $"Error signing-out user: {ex.Message}";
}
}
}
First login
Login again
I have created a chatbot in web channel and direct line.
When i tested in bot emulator i get the right response and when i try to test the same intent in localhost ( webchat) i got different response.
I will show you and example :
call an agent
give me your customer number
( after custemer number was sended ) are you sure ?
if you click Yes ...the data are stored in database ( sql server )
If you do the save in localhost you get : You cancelled the form ( in fact i havent cancell any form
Here is the luisdialog here i call the form :
[LuisIntent("human")]
public async Task human(IDialogContext context, LuisResult result)
{
var form = new FormDialog<Human>(
new Human(),
Human.BuildForm,
FormOptions.PromptInStart,
result.Entities);
context.Call<Human>(form, LossFormCompleted)
}
private async Task LossFormCompleted(IDialogContext context,
IAwaitable<Human> result)
{
HumanCall form = null;
try
{
form = await result;
}
catch (OperationCanceledException)
{
}
if (form == null)
{
await context.PostAsync("You cancelled the form.");
}
else
{
//call the LossForm service to complete the form fill
var message = $"Your data are stored in database";
await context.PostAsync(message);
}
context.Wait(this.MessageReceived);
}
The form model is :
[Serializable]
public class Human
{
[Prompt("What is your contract number?")]
public string contract;
public static IForm<Human> BuildForm()
{
OnCompletionAsyncDelegate<HumanCall> wrapUpRequest = async (context, state) =>
{
using (BotModelDataContext BotDb = new BotModelDataContext())
{
tblBot bot = new tblBot();
bot = BotDb.tblBots.SingleOrDefault(q => q.Reference == state.contract);
if (bot != null)
{
using (bbbserviceSoapClient cws = new bbbserviceSoapClient())
{
viewc a= new viewc();
a.Lastname = bot.Lastname;
}
}
}
};
return new FormBuilder<Human>().Message
("can you send us some info ?")
.Field(nameof(contract))
.OnCompletion(wrapUpRequest)
.Confirm("Are you sure: Yes or No. ")
.Build();
}
}
}
Can someone help me where i'm wrong ? What can i do to retrieve the same response ? It's about timeout problem or what do you thing ?
I do a test based on the code that you provided and make slight modifications, and I find that if some exceptions occur in wrapUpRequest method, it would show "You cancelled the form" instead of the message "Your data are stored in database".
So I suspect that exceptions occurring in wrapUpRequest method (perhaps database query issue or request sent by bbbserviceSoapClient is timeout etc) when you do test via web chat, which causes the issue.
To troubleshoot the issue, you can try to implement/write custom log to detect if any exception occurs within wrapUpRequest method when you test via web chat.
I have an Xamarin.Forms App. In the first step the user of the app has to login on a login page which is a simple ContentPage. After the user succesfully logged in he should see a MasterDetailPage.
My Problem now is that this code line produces a deadlock. So this code line will never finish.
_masterPage.Master = _mainMenuPage;
Here is the whole function:
private void SignInButtonClicked(string username, string password)
{
SignInAsync(username, password).ContinueWith(task =>
{
if (task.Result)
{
_signInPage.StopActivityIndicator();
_masterPage.Master = _mainMenuPage;
_masterPage.Detail = _masterNavigationPage;
MainPage = _masterPage;
}
}, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
}
SignInAsync:
private async System.Threading.Tasks.Task<bool> SignInAsync(string username, string password)
{
bool signedIn = true;
//ToDo SignIn logic
if (signedIn)
{
_mainMenuPage = new CPM.Arda.Mobile.Freelancer.Ui.Pages.MainMenu(this);
await _mainMenuPage.InitAsync();
await _mainMenuPage.RefreshDataAsync();
_mainMenuPage.MainMenuItemSelectedEvent += MainMenuItemSelected;
CPM.Arda.Mobile.Freelancer.Ui.Pages.General.Overview overviewPage = new CPM.Arda.Mobile.Freelancer.Ui.Pages.General.Overview(this);
await overviewPage.InitAsync();
await overviewPage.RefreshDataAsync();
_masterNavigationPage = new Xamarin.Forms.NavigationPage(overviewPage);
_masterPage = new CPM.Arda.Mobile.Freelancer.Ui.Pages.Master(this);
await _masterPage.InitAsync();
await _masterPage.RefreshDataAsync();
}
return signedIn;
}
Any ideas? It seems very strange to me.
I found the solution. I forgot to set the Title property on CPM.Arda.Mobile.Freelancer.Ui.Pages.MainMenu (the MasterPage from the MasterDetailPage). A common mistake with Xamarin.Forms.
But with no exception its very hard to find. Xamarin is not helpful sometimes :/
I have just started working with the OneDrive API and the sample program that comes with it (OneDriveApiBrowser).
As expected, the first time I logged in (using "Sign In to MSA...", I was asked for credentials, my 2-factor code, and finally a permissions screen asking if I approve of the access that the app wants against my OneDrive account.
But, after I exit the program and restart it, I am not logged in. I repeat going to "Sign In to MSA..." and I am no longer prompted for credentials (as I expected), but I am prompted with the permissions screen again.
Is there a way of having the app log back in without always prompting the user for permission?
For learning how to use the OneDrive API, I am just using the sample code that Microsoft supplied as part of the API located at https://github.com/OneDrive/onedrive-sdk-csharp/tree/master/samples/OneDriveApiBrowser. The code can't be downloaded directly from there, but at the root of this project, https://github.com/OneDrive/onedrive-sdk-csharp. This will download the source for the API as well as the sample code and unit tests.
After doing some more poking around, I finally found out how to do this. My explanation here will be in the context of the sample program mentioned in the original question above.
In the program, in the SignIn method, there was some setup being done which included calling OneDriveClient.GetMicrosoftAccountClient(...), then calling the following:
if (!this.oneDriveClient.IsAuthenticated)
{
await this.oneDriveClient.AuthenticateAsync();
}
So, two things needed to be done. We need the save the result from the code above, then save the RefreshToken value somewhere safe... (It's just a very long string).
if (!this.oneDriveClient.IsAuthenticated)
{
AccountSession accountSession = await this.oneDriveClient.AuthenticateAsync();
// Save accountSession.RefreshToken somewhere safe...
}
Finally, I needed to put an if around the OneDriveClient.GetMicrosoftAccountClient(...) call and only call it if the saved refresh token has not been saved yet (or as been deleted due to code added to the logout call...) If we have a saved refresh token, we call `OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(...) instead. The entire SignIn method looks like this when I am done.
private async Task SignIn(ClientType clientType)
{
string refreshToken = null;
AccountSession accountSession;
// NOT the best place to save this, but will do for an example...
refreshToken = Properties.Settings.Default.RefreshToken;
if (this.oneDriveClient == null)
{
if (string.IsNullOrEmpty(refreshToken))
{
this.oneDriveClient = clientType == ClientType.Consumer
? OneDriveClient.GetMicrosoftAccountClient(
FormBrowser.MsaClientId,
FormBrowser.MsaReturnUrl,
FormBrowser.Scopes,
webAuthenticationUi: new FormsWebAuthenticationUi())
: BusinessClientExtensions.GetActiveDirectoryClient(FormBrowser.AadClientId, FormBrowser.AadReturnUrl);
}
else
{
this.oneDriveClient = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(FormBrowser.MsaClientId,
FormBrowser.MsaReturnUrl,
FormBrowser.Scopes,
refreshToken);
}
}
try
{
if (!this.oneDriveClient.IsAuthenticated)
{
accountSession = await this.oneDriveClient.AuthenticateAsync();
// NOT the best place to save this, but will do for an example...
Properties.Settings.Default.RefreshToken = accountSession.RefreshToken;
Properties.Settings.Default.Save();
}
await LoadFolderFromPath();
UpdateConnectedStateUx(true);
}
catch (OneDriveException exception)
{
// Swallow authentication cancelled exceptions
if (!exception.IsMatch(OneDriveErrorCode.AuthenticationCancelled.ToString()))
{
if (exception.IsMatch(OneDriveErrorCode.AuthenticationFailure.ToString()))
{
MessageBox.Show(
"Authentication failed",
"Authentication failed",
MessageBoxButtons.OK);
var httpProvider = this.oneDriveClient.HttpProvider as HttpProvider;
httpProvider.Dispose();
this.oneDriveClient = null;
}
else
{
PresentOneDriveException(exception);
}
}
}
}
For completeness, I updated the logout code
private async void signOutToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.oneDriveClient != null)
{
await this.oneDriveClient.SignOutAsync();
((OneDriveClient)this.oneDriveClient).Dispose();
this.oneDriveClient = null;
// NOT the best place to save this, but will do for an example...
Properties.Settings.Default.RefreshToken = null;
Properties.Settings.Default.Save();
}
UpdateConnectedStateUx(false);
}
The wl.offline_access scope is required by applications to save the user consent information and refresh the access token without a UI prompt.
For more details on the scopes that you can use in your application see https://dev.onedrive.com/auth/msa_oauth.htm#authentication-scopes
so I have succeeded in connecting my Windows Phone 8 Application to the Live API, I also succeeded in reading data from my hotmail account.
I have access to the needed client ID and the live access token.
But when I quit and restart my application, I lose all references to the session and the client objects and I have to start the process anew.
I don't want to annoy the user with the web mask in which he has to agree again that he provides me with the needed permissions every time he is starting the application. But I also haven't found a way to get a reference to a session object without this step.
The login mask is only shown the first time after installing the application, after that, only the screen mentioned above is shown.
But it is still quite annoying for the user to accept this every time.
I already tried serializing the session object, which is not possible, because the class does not have a standard constructor.
Maybe it is possible to create a new session by using the live access token, but I haven't found a way to do so.
Any ideas? What am I doing wrong, I know that there is a way to login again without prompting the user.
I'm thankful for every idea.
Some code I use:
/// <summary>
/// This method is responsible for authenticating an user asyncronesly to Windows Live.
/// </summary>
public void InitAuth()
{
this.authClient.LoginCompleted +=
new EventHandler<LoginCompletedEventArgs>(this.AuthClientLoginCompleted);
this.authClient.LoginAsync(someScopes);
}
/// <summary>
/// This method is called when the Login process has been completed (successfully or with error).
/// </summary>
private void AuthClientLoginCompleted(object sender, LoginCompletedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
LiveConnector.ConnectSession = e.Session; // where do I save this session for reuse?
this.connectClient = new LiveConnectClient(LiveConnector.ConnectSession);
// can I use this access token to create a new session later?
LiveConnector.LiveAccessToken = LiveConnector.ConnectSession.AccessToken;
Debug.WriteLine("Logged in.");
}
else if (e.Error != null)
{
Debug.WriteLine("Error signing in: " + e.Error.ToString());
}
}
I have tried to use the LiveAuthClient.InitializeAsync - method to login in background after restarting the application, but the session object stays empty:
// this is called after application is restarted
private void ReLogin()
{
LiveAuthClient client = new LiveAuthClient(LiveConnector.ClientID);
client.InitializeCompleted += OnInitializeCompleted;
client.InitializeAsync(someScopes);
}
private void OnInitializeCompleted(object sender, LoginCompletedEventArgs e)
{
Debug.WriteLine("***************** Inititalisation completed **********");
Debug.WriteLine(e.Status); // is undefined
Debug.WriteLine(e.Session); // is empty
}
Does anyone have an idea how I could get access to a new session after restarting the application?
After two full days searching for the mistake I was making, I finally found out what I was doing wrong: I have to use the wl.offline_access scope to make this work!
Let me quote another user here:
"If your app uses wl.offline_access scope than the live:SignInButton control saves it for you and loads it automatically. Just use the SessionChanged event to capture the session object. This way the user will need to sign in only once."
(see WP7 how to store LiveConnectSession during TombStoning?)
Now everything is fun again. Can't believe that this was the problem. Tested & working. Nice!
Been struggling to get this working on a Windows Live + Azure Mobile Service app myself so thought I would post a complete working code sample here now that I've got it working.
The key parts are the wl.offline_access scope and the call to InitializeAsync.
Note: this sample also connects with Windows Azure Mobile Services. Just remove the stuff related to MobileService if you're not using that.
private static LiveConnectSession _session;
private static readonly string[] scopes = new[] {"wl.signin", "wl.offline_access", "wl.basic"};
private async System.Threading.Tasks.Task Authenticate()
{
try
{
var liveIdClient = new LiveAuthClient("YOUR_CLIENT_ID");
var initResult = await liveIdClient.InitializeAsync(scopes);
_session = initResult.Session;
if (null != _session)
{
await MobileService.LoginWithMicrosoftAccountAsync(_session.AuthenticationToken);
}
if (null == _session)
{
LiveLoginResult result = await liveIdClient.LoginAsync(scopes);
if (result.Status == LiveConnectSessionStatus.Connected)
{
_session = result.Session;
await MobileService.LoginWithMicrosoftAccountAsync(result.Session.AuthenticationToken);
}
else
{
_session = null;
MessageBox.Show("Unable to authenticate with Windows Live.", "Login failed :(", MessageBoxButton.OK);
}
}
}
finally
{
}
}