javafx Do a http post request in webenigne - javafx

I have googled around but unfortunately I didnt find what I am looking for. For a small application I am working on, I am using a Webview. In this webview I would like to load a page where I would like to do the login - which is done using a http post request of type
--> "Content-Type", "multipart/form-data;boundary="+boundary; <--
Using a HttpURLConnection I can use a DataOutputStream passing the login data to the server. But how would I do such a login with on a WebView's WebEngine??
Best Regards

Load the page in the WebEngine and have the user enter their login credentials and submit them just like they would in a standard browser. The WebEngine will do the multi-part form data post the server to appropriately log the user in.
If you need to automate this process on behalf of the user (i.e. because you have authentication credentials stored elsewhere for a kind of single sign on app), then, once the engine has fully loaded the login page, you can make webEngine.executeScript calls to populate the form login fields on the page and trigger the submission of the login information to the server (probably by triggering a click event on the login form submission button).
You can also use this routine to execute jQuery on the document to do the form filling and trigger the submission if you prefer to work with that API.

Related

How to display a loading icon when logging into Blazor server application?

I have a Blazor server application (great) with a Razor page login form (absolute pain in my backside).
Upon submitting the login form, in the PageModel class I have an OnPostAsync method which calls upon my API to sign in and obtain an auth token, but obviously there is an element of delay to this (quite bad in my case at the moment). As it is, my page shows almost no indication that it is doing anything at all after pressing login, whilst the API is being contacted the page sits there like the submit button hasn't even been pressed, which is obviously very bad for users because the first thing they will do is mash the submit button until they get a visual response (as I probably would to be fair).
How on earth do I do this in a Razor page? I've tried this and this, neither have worked. At the moment I don't particularly care what is shown or done, I just want it to do SOMETHING. The perfect solution for me is disabling the form and/or overlaying something to show that it's busy, but I'd take a basic "Loading..." message somewhere.
Is there any way of doing this?
It seems that it simply isn't possible to do with a Razor page in a Blazor server application, because there is nothing to update the UI, it's not like a Blazor component where there is a SignalR connection kept active between the server and the client. In the case of the login page, the page is served to the client and the connection is then severed, so the next action that occurs is the page submits the details it's holding to the server and expects the login result as a response.
Javascript should have worked and I don't know why it didn't, I added a script to change some of the UI elements upon submission of the form, but it seems the OnPostAsync method in the PageModel was being called first and whether the script was running or not it just wasn't updating the UI, I'm guessing because it's awaiting the response before doing anything. The alternative was to remove the login code from the PageModel and use an AJAX call instead, but I didn't really want to do this.
I've added an ILoginHandlerService, with a StoreLogin and RetrieveLogin. In the implementation it receives a login object containing the credentials, encrypts them and stores them in a private variable. This service is added as a singleton in the app startup class. The login page now calls the StoreLogin method with the submitted details, and redirects to a new "Logging in" page. This new page has a "form" with just a text element containing a please wait message, and a script which calls Post on the form upon loading.
The "Logging in" page's OnPostAsync is what actually performs the login. It calls upon an injected IAuthService to log in, without passing any details to it. The IAuthService implementation itself (added as a scoped service in app startup) gets injected with the ILoginHandlerService, then internally calls upon that service to supply the stored login object. The stored object is decrypted, nulled and returned to the IAuthService and is then submitted to my API, which then responds with the logged-in user token, that is returned to the "Logging in" page, which then calls upon HttpContext.SignInAsync, adding the received API token, and redirects the user to the home page, signed in.
This seems very convoluted to me and I'm not entirely sure it's correct. From what I understand of the above, once the credentials are submitted from the login form all future processing of the credentials should now be done server side and are never returned back to the client?

Infinite redirect loop after facebook authenication in asp.net

I am trying to use facebook authentication on my asp.net application.
I am using Facebook JavaScript SDK calls for this purpose.
In my case, I call the facebook javascript SDK from my normal Default.aspx page, then it authenticates, then sends the uid and access token to a data handler I created. In the datahandler I am creating the session. Then I redirect to the parent page from the data handler.
All this works fine when I am using a normal ASPX page (not any usercontrol). But when I use the same logic inside any user control and call the user control from the default.aspx page (in this case the facebook authentication and facebook login button is inside my user control), after authentication and calling data handler, when I redirect to my default.aspx from my datahandler, this cycle goes on infinitely.
My page keeps on authenticating and keeps on calling the datahandler when then again calls the default.aspx
Please suggest any good approach where I can authenticate the user and come back to my default.aspc when it has the user control.
I created my facebook authentication logic (used only the javascript and datahandler logic) by following this tutorial:
http://csharpsdk.org/docs/web/getting-started
I can attach the code also if requested.

Forms Authentication issue with WebBrowser Class in asp.net

I am developing a site and in this site i m building a functionality to capture a screen shot of a page. In my site i am using forms authentication.now first of all.
So when user login to the website he/she has to enter the credentials and then go to their profile page.
So now i am just creating a thumbnail of the user profile page using WebBrowser Control but the problem is , i m using forms authentication and it's always capture the login forms page because of forms authentication.
So Please help me ASAP.
In that case you will need to perform login programtically for forms authentication. Here is the piece of code which will be required.
FormsAuthentication.SetAuthCookie(UserName, false);
And here is more info
http://weblogs.asp.net/joseguay/archive/2009/03/23/the-asp-net-capsule-2-login-programmatically-with-forms-authentication.aspx.
However, be aware that the code you are using for creation of screenshot might not go well with it. So take care of this. These posts might help you in that case Send credentials to WebBrowser
You have to simulate forms authentication via WebBrowser control - essentially, use document object model (Document property) to locate user name/password input boxes, set their values and trigger submit (either inject java-script to do form submit OR use DOM to simulate login button click).
IMO, better way would be to use WebRequest (HttpWebRequest) to simulate the POST to login page to do authentication and then issue request to user profile page. Get the page html(from response) and load it in Web Browser control using DocumentText property.
In case, you have control over the server site, you may modify user profile page to allow un-authenticated access over certain requests (for example, from local machine or specified IP etc).

How to invoke a Forms Auth login modal dialog instead of redirecting to a loginUrl signin page?

I've just finished implementing a modal dialog login popup for my ASP.NET website. The experience is similar to what you get with DIGG.com. When you log in, you get a fancy popup modal dialog (provided by the JQuery Tools Overlay control). The dialog is an ASCX file in the Master Page, so it's available globally. It uses a PageMethod to validate the current user. If the validation succeeds, I call window.location.reload(); in the PageMethod's success callback.
This works great when logging in on a page that doesn't require authentication, but what about when non-auth users are trying to navigate to a page that requires auth?
Is it possible to modify my web.config file so that instead of redirecting to a SignIn.aspx page for non-authenticated users I simply invoke the modal dialog instead?
Let's say a non-authenticated user is on Default.aspx which doesn't require auth. He wants to navigate to "Add.aspx" which does require auth. What's the best way to handle this with a modal dialog popup?
If I have to use a dedicated page, I guess I'll just have a SignIn.aspx page that invokes the dialog when it loads and if authentication succeeds, it'll use JavaScript to redirect to the destination page.
But ideally, I'd like to do the login from the Default.page and then redirect to "Add.aspx" with script.
Since I didn't get any feedback and went ahead and "solved" this issue conventionally. I still have a dedicated Login.aspx page but it's only used in cases where users navigate directly to a page requiring authentication.
In this case, I let ASP.NET redirect to the Login.aspx, and then using client script I invoke the popup dialog. I do the normal authentication using a WebMethod and then redirect as needed. If the user cancels the dialog, I just redirect back to Default.aspx.
Seems like there's no getting around the need for a dedicated Login page.

Open protected web page passing in credentials programmatically

I have code examples from some of my previous work that help me to post form values to a web page (login credentials) and retrieve the text from that page. Now I want to pass in form values (login credentials again) but actually open that web page in a browser given those credentials.
How do I do that? I'm not doing anything nefarious. In our CRM app (home-grown as it is), I want to create a link button that opens our web site's protected products page given the user's credentials (based on the user's login credentials). Normally, I'd copy the user's credentials in our login page which then takes me to the products page. I'm trying to do this now by just clicking a link button.
Any suggestions?
How are you launching the browser? Is this an internal network app? If so, I would recommend using Windows Authentication for your ASP.NET app, and then you don't have to worry about passing credentials. If you can't do that, then you'll probably have to pass the credentials on the querystring generated by your CRM app. Obviously, this is a huge security risk. But the next step would be to perform your internal authentication and then call FormsAuthencation.RedirectFromLoginPage or FormsAuthentication.SetAuthCookie().

Resources