How can I make AuthenticationContext.AcquireTokenAsync() use a modal dialog - wpf

I am using AuthenticationContext in a WPF app to authenticate a user to Azure Active Directory. It works great as expected,
but the login dialog shown by the following code is not modal, meaning that it can easily end up hidden behind my WPF app.
Is there a way to force it to be modal?
var result = await authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior.Always));
Update:
I am able to set the ownerWindow property of the PlatformParameters by supplying the window returned by
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
But this results in an error:
Invalid owner window type. Expected types are IWin32Window or IntPtr
However, I have since discovered that the reason my dialog was not modal is that it's appearing before my main window has loaded. If my main window loads first, then the dialog is modal without me having to set the ownerWindow property of the PlatformParameters.

Related

Page Navigation Controls in Report Viewer Toolbar is not Functioning Properly

I have a working report viewer function in a .net 4.5 Winforms application. Recently I moved the Form page containing the report viewer control to a separate project in order to load it in it's own AppDomain with LegacyCAS security enabled i.e.:
AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory, LoaderOptimization = LoaderOptimization.MultiDomainHost };
setup.SetCompatibilitySwitches(new[] { "NetFx40_LegacySecurityPolicy" });
AppDomain _casPolicyEnabledDomain = AppDomain.CreateDomain("Full Trust", null, setup);
try
{
ReportViewAppDomain.rptSalesReport genSalesReport = (ReportViewAppDomain.rptSalesReport)_casPolicyEnabledDomain.CreateInstanceFromAndUnwrap(typeof(ReportViewAppDomain.rptSalesReport).Assembly.CodeBase, typeof(ReportViewAppDomain.rptSalesReport).FullName);
bool result = genSalesReport.SalesMonthlyReport_Load(Convert.ToDateTime(startTimePicker.Value), Convert.ToDateTime(endTimePicker.Value), stores, storeIds, depts, deptIds, ParentForm);
}
catch (Exception ex)
{
//handle exception here
}
genSalesReport.SalesMonthlyReport_Load() is the call to my Form that generates a report in report viewer local mode. Since loading this Form in its own AppDomain the numeric textbox of the Report Viewer toolbar page navigation control (see image) does not work. I can type a page number in the textbox but when hitting enter on the keyboard the report viewer does not navigate to the specified page.
The page control arrows still work and the page counts and other functions are correct in the navigation control. I've specified this.reportViewer1.ShowPageNavigationControls = true; in the designer file and set focus to the report viewer form page yet the issue still persists. Any suggestions would be greatly appreciated.
I found this researching my own issue and I suspect it's related. We have our WinForms AcceptButton (the Enter key) wired to the View Report button. As such we can only ever have Enter trigger "View Report" or the page navigation, not both. We have to turn off the View-Report-Trigger after the report is rendered the first time or the navigation buttons won't work. Recommend checking the Form that hosts the report to see if you have your Enter Key double-booked.

Save Dialog box in Silverlight

i am working on Silverlight 5,
Application has functionality like save data in user's local pc as CSV. while developing functionality it's working perfect at our local PC. when we click on "Export" Button save file dialog box appear and save at selected location. but after deployed on our server save file dialog box will not appear on screen.
dialog = new SaveFileDialog();
dialog.DefaultFileName = "Exported Data";
dialog.Filter = string.Format("File Type (*{0}) | *{0}", (".csv"));
dialog.DefaultExt = string.Format("{0}", ("csv"));
//Show the dialog
bool? dialogResult = dialog.ShowDialog();
Make sure you call ShowDialog() method right after "Export" button click event, this is Silverlight security feature.
See: http://msdn.microsoft.com/en-au/library/system.windows.controls.savefiledialog(v=vs.95).aspx
You show a save dialog control using the ShowDialog method. For security purposes Silverlight file and print dialogs must be user-initiated. This means you must show them from a user-initiated action such as the click event handler for a button. In addition, there is a limit on the time allowed between when the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur.

Why does an IE window pops up when closing a form on WebBrowser.DocumentCompleted handler

I have a form that has a WebBrowser control. Onload, it navigates to some URL.
On DocumentCompleted event handler, after getting the needed data the form is closed. This works most of the time, but sometime it pops up an IE window w/ the same URL after the form has already closed.
I noticed that in DebugView, it logs m_useSurfacePresenter 1. When this gets logged before the Close() is called, there is no popup. But when this gets logged after the Close() is called, then the popup appears.
You need to check the JavaScript .onunload event in that document, if it is set to open a new window (browser) when that page is unloading, then it will be triggered when you close your winform which is hosting the webbrowser control, and in turn, the page.
So, when you close the winform, the page's onUnload event in the webbrowser control will also be triggered as a result due to the way the events are chained in a Document/Page --> WB Control --> WinForms Form setup.
You can actually set this to nothing, by running a particular JS command directed to the page in the WB control, you can set the .OnUnload event to "function () {};", which will ensure whatever it is set to will be turned into Nothing/Null. Here is a code example:
window.onunload = function () { };
Let me know how it goes, and I will help you out if that doesn't fix it. Notice the above is JS, and there are ways to execute JS directly from teh WB Control, many examples on the web, there are actually 2 ways, one using .execScript and another one using the newer method, which is .InvokeScript.

WinForms modal windows alt+tab problem

Suppose multiple Modal Windows shown above each other.
All of those have ShowInTaskbar = false, which means that in the TaskBar you only see the MainForm and all Modal Windows are hidden.
Now you press ALT+ TAB and the most upper modal Windows disappears. But you cannot get it back in front.
How should be this done correctly in your opinion?
If a modal window is getting stuck behind the main form, it sounds like you are not setting its owner. When you call showDialog(), you need to pass in the main form like this:
modalWin.showDialog(mainForm);
Any time you call showDialog(), and your program has another form that should be underneath, it is best to pass it as the owner. If you show modal window when there is already a modal window up, then pass the first modal window as the owner.
OK Just to complete it:
This is how to set an Owner to be a Winform for a Winform:
form.ShowDialog(ownerInstance);
This is how to set an Owner to be a Winform for a WPF Window:
MyWpfDialog dialog = new MyWpfDialog();
new System.Windows.Interop.WindowInteropHelper(dialog).Owner = ownerInstance.Handle;
dialog.ShowDialog();
This is how to set an Ownder to be a Wpf Window for a Wpf Window:
.Owner = Window.GetWindow(ownerInstance)

How to Programatically "Click" a Silverlight HyperlinkButton (WebAii)

I'm currently using the WebAii automation framework to write some user interface tests against a Silverlight 3 application. I'm new to Silverlight and suspect that I'm missing some bit of information about the HyperlinkButton.
The application has a HyperlinkButton and I'm attempting to write code that navigates to the page, finds the button the page, then "clicks" that button (which will then navigate to the NavigateUri as specified in the HyperlinkButton's properties).
I can't figure out how to execute that click. The code I have thus far (simplified):
Manager.LaunchNewBrowser(BrowserType.InternetExplorer);
ActiveBrowser.NavigateTo("http://server/appname/");
var slApp = ActiveBrowser.SilverlightApps()[0];
var menu = slApp.FindName<StackPanel>("LinksStackPanel");
var linkicareabout = menu.Find.ByName<HyperlinkButton>("Some Cases");
I'd expect to see some sort of Click() action, or Navigate() method that I could invoke on the "linkicareabout" variable, but I must be missing how it's done.
What you are looking for is the User object off the HyperlinkButton. All controls that WebAii comes with have that object. This way you can invoke any user action on any control type.
linkicareabout.User.Click()
The User object supports any user action you can think of and mimic real user interactions. Check out the documention here.
I was unable to do this myself and instead, had to write my own navigation code. For Firefox and IE, you can just use HtmlPage.Window.Navigate to navigate to the desired URL.
However, Safari and Chrome need some extra work. I had to use hidden HTML components and some javascript interops.
This workaround is detailed here.
Basically, it entails adding a hidden anchor and button to the HTML page containing your Silverlight control, and then modifying the anchor and clicking the button via calls to the DOM.
HtmlElement anchor = HtmlPage.Document.GetElementById("externalAnchor");
HtmlElement button = HtmlPage.Document.GetElementById("externalButton");
if ((anchor != null) && (button != null))
{
anchor.SetProperty("href", url);
button.Invoke("click", null);
}

Resources