GET WebView's WebNavigatedEventArgs Result Error Code when successful in .NET MAUI - webview

It seems that when Navigated event occurs, the Result from the navigation of the WebView in .NET MAUI is successful, no matter that the response status is 401 for example. (Unauthorized).
I was counting that it will be Failure, for anything but 2xx.
Is there a way to detect problem when loading the WebView?
(Perhaps without writing too much platform specific code)
Edit(adding some related code):
void Navigated(WebNavigatedEventArgs args)
{
if (args.Result == WebNavigationResult.Failure)
{
IsConnectionOK = false;
}
}
The Result here is success, not failure.
The HTTP response status of the request is 401. (Unauthorized)

Related

Dart request succeeding ... somehow?

I'm developing a dart application which will consume a REST service I'm building. I started writing out the dart code to perform an ajax request to my login endpoint. However, even when my dart ajax request should fail, it claims to succeed.
I don't have any services up and running (and even if I did it would be using the wrong domain / port right now), but this code gives a 200 OK HttpResponse every time:
class PlayerController {
const PlayerController();
static const String LOGIN_URL = "login";
void login(String username, String password) {
Map<String, String> headers = {"Content-Type": "application/x-www-form-urlencoded"};
String body = "j_username=$username&j_password=$password&submit=Login";
HttpRequest.request(LOGIN_URL, method: "POST", requestHeaders: headers, sendData: body)
.then((request) => processLogin(request, username))
.catchError((e) => processLoginError(e));
}
void processLogin(var whatIsThis, String username) {
query("#loginButton").text = "Logout";
//TODO get the player then set them
}
void processLoginError(var e) {
print("total failure to login because of $e");
}
}
It always hits the processLogin method, and never hits the processLoginError method. Does anyone have any idea why this would be? Should I be performing this ajax request in a different way? (If you couldn't guess, it will be signing into spring security).
I read somewhere that file system requests always succeed. Is Dart somehow making this a file system request rather than a web request?
This is because the request actually completes successfully.
Your request to "login" will actually call http://127.0.0.1:6521/[Path_to_your_Dart_file]/login
The server started by Dart when running in Dartium (127.0.0.1:6521) seems to answer to every POST request with HTTP 200 and an empty response body.
If you change the method from POST to GET, it will fail as expected.
As for why the server does this - I don't really know. This would have to be answered by the Dart team.

AppHarbor MVC - Unable to use HttpNoContent Status Code

I need to be able to respond to requests with a http status code of 204 but appharbor is only returning a 500 error. My controller code is executing correctly but when the code below is called, I only see a 500 error in fiddler.
protected ViewResult HttpNoContent()
{
Response.StatusCode = (int)HttpStatusCode.NoContent;
return View("NoContent");
}
Quote from the specification (I have put the important part in bold):
The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
You are not respecting this rule. 204 status code means no content and yet you are returning a view. Try returning an EmptyResult:
protected ViewResult HttpNoContent()
{
Response.StatusCode = (int)HttpStatusCode.NoContent;
return new EmptyResult();
}

Jsf2.0 preRenderView

i am using jsf 2.0
i have question accoring to PreRenderView.
in my Bean i have method like
public void init() throws Exception
{
FacesContext.getCurrentInstance().getExternalContext().redirect("/To My Page");
if(!FacesContext.getCurrentInstance().isPostback())
{
System.out.println("Kshitij");
}
}
when this method is executing it is also printing "Kshitij" in servler log.
then redirecting to page.
why? i think it has to redirect to page first.
Why do you think that the actual redirect is performed first? The method has to finish running first before the server can ever continue the control over the request/response. It's impossible to pause code execution halfway and then continue code execution at exactly the same location in a brand new request and thread.
The redirect() call basically sets the Location response header. Only when the method has returned, the server will send the response and then the browser will send a new request on that location.
Add a return statement or an if/else if you want to skip the printing when you need to redirect.
if (youNeedToRedirect) {
FacesContext.getCurrentInstance().getExternalContext().redirect("/To My Page");
}
else {
if (!FacesContext.getCurrentInstance().isPostback()) {
System.out.println("Kshitij");
}
}
This all has got nothing to do with JSF or preRenderView. It's all just basic Java and HTTP.
Related:
java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

Custom basic authentication fails in IIS7

I have an ASP.NET MVC application, with some RESTful services that I'm trying to secure using custom basic authentication (they are authenticated against my own database). I have implemented this by writing an HTTPModule.
I have one method attached to the HttpApplication.AuthenticateRequest event, which calls this method in the case of authentication failure:
private static void RejectWith401(HttpApplication app)
{
app.Response.StatusCode = 401;
app.Response.StatusDescription = "Access Denied";
app.CompleteRequest();
}
This method is attached to the HttpApplication.EndRequest event:
public void OnEndRequest(object source, EventArgs eventArgs)
{
var app = (HttpApplication) source;
if (app.Response.StatusCode == 401)
{
string val = String.Format("Basic Realm=\"{0}\"", "MyCustomBasicAuthentication");
app.Response.AppendHeader("WWW-Authenticate", val);
}
}
This code adds the "WWW-Authenticate" header which tells the browser to throw up the login dialog. This works perfectly when I debug locally using Visual Studio's web server. But it fails when I run it in IIS7.
For IIS7 I have the built-in authentication modules all turned off, except anonymous. It still returns an HTTP 401 response, but it appears to be removing the WWW-Authenticate header.
Any ideas?
I figured it out. The problem was that I named this module, "BasicAuthenticationModule" which conflicted with another module IIS had built in. Once I renamed the module things worked just fine!
Even though you have it working, this is something else to consider:
http://wcfrestcontrib.codeplex.com/wikipage?title=Web%20Authentication%20Overview&referringTitle=Home

How can send back my own 404 error message in ASP.NET , but as json?

i'm trying to send back a simple error message as Json, with the HTTP code as 404.
So i started out writing my own IExceptionFilter that checks to see the exception. To keep this simple, if the exception throw is of type ResourceNotFoundException then i set the code to 404. Otherwise everything else if 500.
Now, the problem is .. the default IIS7 404 error message is returned :( my code is called .. but it seems to bypass it (later on in the pipeline)...
is there some trick i need to do?
do I need a custom error handling (in the web config) to be turned on or something?
Edit:
I'm trying to do what twitter does. Their Http Response Code documentation shows / explains some examples how they handle 404's, etc.. and i'm wanting to do that in my MVC app.
Edit 2:
The code i've done is listed here, for anyones reference :)
When you are handling your exception, are you setting ExceptionHandled to true?
Here's a quick example...
HandleException(ActionExecutedContext filterContext)
{
Exception exception = filterContext.Exception;
//Check if our exception has been handled.
if (filterContext.ExceptionHandled == false)
{
//Do your exception stuff
filterContext.Result = YourExceptionMessageAsAnActionResult();
//Set it as null.
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
}
}

Resources