Setting AcceptUnsignedLogoutResponses in web.config for Sustainsys.Saml2 - web-config

I'm trying to enable this AcceptUnsignedLogoutResponses setting that was added here https://github.com/Sustainsys/Saml2/commit/22f1605eba659641a5a46edb20458b1b050c93af in order to handle Identity providers that don't sign their logout messsages.
I've tried to add a new element <compatibility AcceptUnsignedLogoutResponses="true"></compatibility> in the <sustainsys.saml2> element in web.config, but then I get a "System.Configuration.ConfigurationErrorsException" error stating that the attribute isn't recognized.
I could try to assign it by code as it's recognized this way
var compatibility = new Sustainsys.Saml2.Configuration.Compatibility();
compatibility.AcceptUnsignedLogoutResponses = true;
but I have no idea how to alter the current configuration to set just this setting.
Does anyone know how to set it in web.config or the way to do it in code (WebForms)?

You can do it by modifying your global.asax:
<%# Application Language="C#" %>
<script RunAt="server">
public void Application_Start(object sender, EventArgs e)
{
var config = Sustainsys.Saml2.Configuration.Options.FromConfiguration;
config.SPOptions.Compatibility.AcceptUnsignedLogoutResponses = true;
}
</script>

Related

.NET MAUI Community Toolkit Popup PopupHandler is incompatible

I started working with .NET MAUI. I ran into a problem just by starting my development.
I want to show a popup and I'm using the Community Toolkit.
All I did is:
I created a new .NET MAUI Application Project, installed the Community Toolkit NuGet Package (of course also the .UseMauiCommunityToolkit in the start up class) and added a XAML File for the Popup:
<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="TestApp.ProfilePopup">
<VerticalStackLayout>
<Label Text="This is a very important message!" />
</VerticalStackLayout>
</toolkit:Popup>
I've no partial class for this popup
I just modified the button on MainPage to display the popup:
private void OnCounterClicked(object sender, EventArgs e)
{
var popup = new ProfilePopup();
this.ShowPopup(popup);
}
If I run this application and click on the button to display the popup I'll get the error message:
CommunityToolkit.Maui.Core.Handlers.PopupHandler found for TestApp.ProfilePopup is incompatible
If I create the popup in C#, it works:
private void OnCounterClicked(object sender, EventArgs e)
{
var popup = new Popup
{
Content = new VerticalStackLayout
{
Children =
{
new Label
{
Text = "This is a very important message!"
}
}
}
};
this.ShowPopup(popup);
}
Any idea what I'm doing wrong?
Thank you!
Markus
I reproduced the error message.
THE CAUSE: "I've no partial class for this popup".
That won't work. without that, there is no InitializeComponent call. The result is not a valid View.
To fix the problem,
First make sure you have got the toolkit registered in MauiProgram.cs:
using CommunityToolkit.Maui;
...
builder.UseMauiApp<App>().UseMauiCommunityToolkit();
then you must have
file ProfilePopup.xaml.cs containing:
public partial class ProfilePopup : CommunityToolkit.Maui.Views.Popup
{
public ProfilePopup()
{
InitializeComponent();
}
}
I generate custom views using these steps:
Project / Add New Item / .NET MAUI ContentView (XAML).
Give name "MyView". This adds TWO files to project: MyView.xaml and MyView.xaml.cs.
In MyView.xaml, add needed xmlns and change base class.
Was:
<ContentView xmlns=...
...
x:Class=...>
...
</ContentView>
change to:
<toolkit:Popup xmlns=
...
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class=...>
...
</toolkit:Popup>
In MyView.xaml.cs, change base class.
Was:
public partial class ProfilePopup : ContentView
change to:
public partial class ProfilePopup : CommunityToolkit.Maui.Views.Popup
The documentation on Microsoft page should be completed (I used as template):
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup
I was having the same problem. I forgot to add .UseMauiCommunityToolkit(); to MauiProgram.cs.
You can try this, as well:
var popup = new PopupPage();
await PopupExtensions.ShowPopupAsync<PopupPage>(this, popup);

Blazor - ondrop event doesn't return file object

I have bind #ondrop event into my div element, while drag and drop local image into div, DragEventArgs doesn't return file object details.
Razor code
<div #ondrop="#OnDrop"></div>
#code{
internal void OnDrop(DragEventArgs args)
{
var files = args.DataTransfer.Files;
}
}
output:
Anyone have solution for this case?
Note: My main goal is to convert it as FileStream or MemoryStream then save it into specified location
You should use preventDefault here:
<div #ondrop:preventDefault="#OnDrop"></div>
and maybe you still have to handle Dragover as well, I'm not sure.

Mutually Exclusive Access in ASP.net + C#.net

Currently I am facing a problem which I am unable to solve after a lot of work and search, I asked a similar question before but didn't got any reply maybe because I didn't asked it correctly so I deleted that question.
Ok I am downloading emails using MailBEE.net Objects library and it is working fine except that if downloading method is called again while a previous call is still in downloading phase then two copies of messages start to download which is wrong.
on an ASP.net page I am calling an ASHX handler that downloads the emails
public class sync : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
ApplicationData.layer Functions = new layer();
context.Response.ContentType = "text/text";
int messageCount = Convert.ToInt16(context.Request.QueryString["messageCount"]);
if (Functions.SyncMail("email", "user", "password", "pop.gmail.com", messageCount) == "Successful")
{
context.Response.Write("New Messages Downloaded.");
}
//context.Response.Write("Hello World");
}
}
I am calling the above ASHX handler using Jquery from another (ASPX) page
<script type="text/javascript">
$(document).ready(function() {
asyncLoad();
});
function asyncLoad() {
document.getElementById("CustomerDetails").innerHTML = "<img alt='' src='Images/ajax-loader.gif' />" + " <span>Downloading New Messages...</span>";
$('#CustomerDetails').load("sync.ashx?messageCount=" + "10");
callAgain();
}
function callAgain() {
setInterval(asyncLoad, 20000);
}
</script>
The purpose is to keep on calling sync.ashx after some time (The 20000 delay) to check new messages, the problem is if one call of sync.ashx is busy in downloading messages and a new call is made during this time, it starts to download identical copies of messages since it does not find the ids of emails in database which previous call is going to make.
I need some sort of mutually exclusive access, that if once call is busy in downloading messages then another call should not be made.
Something like
if(IsAlreadyDownloading == False)
{
Functions.SyncMail(params)
}
Where IsAlreadyDownloading is a global flag or mutex that should be set True once one call start downloading and be set false once it finish downloading or some exception has occurred indicating that another call can be made safely.
Since it is an ASP.net application we don't know when user will navigate away from the page which start download call and when it will navigate back to that page so another call to download handler should be made or not.
I don't know if I explained it properly or not but I hope someone will understand. Thank you.
Finally after much hair pulling and head scratching I managed to solve this problem, I don't know if it's the best solution but at least it is working properly now, if you know a better solution please share it here, thanks.
Here is my solution.
I used global.asax to declare application level global variable named as follows
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["IsAlreadyDownloading"] = false;
}
The purpose is to mimic mutex like behavior and for that I left sync.ashx file and made another an aspx file, the reason for shifting from ashx file is access application level variable IsAlreadyDownloading which is unavailable in a generic handler. The code of aspx file is as follows
<%# Page Language="C#" %>
<%# Import Namespace="ApplicationData" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
int messageCount = Convert.ToInt16(Request.QueryString["messageCount"]);
ApplicationData.layer Functions = new layer();
try
{
if (!Convert.ToBoolean(Application["IsAlreadyDownloading"]))
{
Application["IsAlreadyDownloading"] = true;
if (Functions.SyncMail("email", "user", "password", "pop.gmail.com", messageCount) == "Successful")
{
Response.Write("New Messages Downloaded.");
Application["IsAlreadyDownloading"] = false;
}
else
{
Response.Write("<p>Unable to download some messages</p>");
Application["IsAlreadyDownloading"] = false;
}
}
}
finally
{
Application["IsAlreadyDownloading"] = false;
}
Response.End();
}
</script>
No matter how many times we make call to this page the method will not work as it will find IsAlreadyDownloading = true if another call of method is busy in work, as soon as it will finish it will again set IsAlreadyDownloading = false so a new call can acquire it.
Hope this help someone in similar problem.
server

Writing script src dynamically via wicket

I want my page to load javascript dynamically to my body:
<script type= "text/javascript" src="this path should be decided from wicket dynamically"/>
I am using wicket version 1.4 therefore JavaScriptResourceReference does not exist in my version (for my inspection it wasn't ' )
how can I solve this ?
thanks in advance :).
I specify my comment into an answer.
You can use this code snippet:
WebMarkupContainer scriptContainer = new WebMarkupContainer("scriptContainer ");
scriptContainer .add(new AttributeAppender("type", Model.of("text/javascript")));
scriptContainer .add(
new AttributeAppender("src", urlFor(
new JavaScriptResourceReference(
YourClass.class, "JavaScriptFile.js"), null).toString()));
add(scriptContainer );
and the corresponding html:
<script wicket:id="scriptContainer "></script>
Just change the string JavaScriptFile.js to load any other Javascript file.
JavascriptPackageResource.getHeaderContributor() does exactly what you need.
You need nothing in your markup, just add the HeaderContributor it returns to your page.
Update: For Wicket 1.5 see the migration guide, but it goes like this:
public class MyPage extends WebPage {
public MyPage() {
}
public void renderHead(IHeaderResponse response) {
response.renderJavaScriptReference(new PackageResourceReference(YuiLib.class,
"yahoo-dom-event/yahoo-dom-event.js"));
response.renderCSSReference(new PackageResourceReference(AbstractCalendar.class,
"assets/skins/sam/calendar.css"));
}
}
If you want to put your <script> element in the body, you can simply declare it as a WebMarkupContainer and add an AttributeModifier to set the src attribute. Although in that case wicket won't generate the relative URLs for you, you have to do it yourself.
I'm not sure I understood completely.
If you are trying to create and append a script to the body after the page is loaded you should do it this way:
<script type="text/javascript">
function load_js() {
var element = document.createElement("script");
element.src = "scripts/YOUR_SCRIPT_SRC.js"; // <---- HERE <-----
document.body.appendChild(element);
}
// Wait for the page to be loaded
if(window.addEventListener)
window.addEventListener("load",load_js,false);
else if(window.attachEvent)
window.attachEvent("onload",load_js);
else
window.onload = load_js;
</script>
What I did here is create a new script element, and then apply to it its source.
That way you can control dynamicaly the src. After that I append it to the body.
The last part is there so the new element is applied only after the page is loaded.

MVC 2 Templates - Is it possible to redirect the current editor template to its display template based on metadata?

For a project I'm working I've been asked to redirect an editor template to its display template based on metadata that is provided with the model.
Now I was looking at a way to do it before it hits the editor template but that seems to cause more issues than it is worth, at least with how the system has been architected.
The simplest example is the string editor, its a simple textbox but if IsReadOnly is set we want it to only show up as text, not a disabled textbox.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (this.ViewData.ModelMetadata.IsReadOnly)
{
Response.Write(Html.DisplayForModel());
}
else if (this.ViewData.ModelMetadata.ShowForEdit)
{
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { #class = "text-box single-line" }) %>
<% } %>
So far the only real solution I can find is to copy the display template into the editor template. Does anyone have any ideas how I can do something that will work without replicating more code?
Why not do this outside of the editor template itself? Define an extension method that checks if the property is read only then either shows the edit or display template. You'll need to copy the PropertyHelper class from this answer.
public MvcHtmlString DisplayOrEditFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> selector)
{
var property = PropertyHelper<TModel>.GetProperty(selector);
if(property.CanWrite)
{
return helper.EditorFor(selector);
}
return helper.DisplayFor(selector);
}
Then in your view just do
<%: Html.DisplayOrEditFor(x => x.Name) %>
Only drawback is this won't work with Html.EditorForModel().