Forum Discussion
Custom Security Extension using Forms Authentication sending invalid URI on redirect
- 1 year ago
I figured out a solution. It's more of a workaround though.
I learned, without any specific details, that Windows Server 2022 datacenter has much more restrictive policies around URL validation and allowable query strings. My custom extension only needs the query string in order to determine where to redirect the user back to PowerBI once they are authenticated. Instead of trying to capture this in my Page_Load method, I moved that code to the OnInit method at the very beginning. I first check for the ReturnUrl parameter and if present I capture the url parameter from it into a cookie and then immediately redirect to my Logon.aspx page again without any query string.
Here's an example of the code:
override protected void OnInit(EventArgs e)
{
var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
var returnUrl = HttpUtility.ParseQueryString(uri.Query).Get("ReturnUrl");
if (returnUrl != null)
{
var returnUri = new Uri("https://dummy.com" + returnUrl);
var encodedRedirectUrl = returnUri != null ? HttpUtility.ParseQueryString(returnUri.Query).Get("url") : null;
var decodedRedirectUrl = encodedRedirectUrl != null ? HttpUtility.UrlDecode(encodedRedirectUrl) : null;
if (decodedRedirectUrl != null)
{
var redirectUrlCookie = new HttpCookie("redirectUrl", decodedRedirectUrl)
{
HttpOnly = true,
Secure = true
};
Response.Cookies.Add(redirectUrlCookie);
}
string urlWithoutQueryString = uri.GetLeftPart(UriPartial.Path);
HttpContext.Current.Response.Redirect(urlWithoutQueryString);
}InitializeComponent();
base.OnInit(e);
}This manages to remove the query string from the URL that PowerBI is providing before the Windows Server 2022 restrictions apply to it.
Now my custom extension works on both Windows Server 2019 and 2022.
I had a new server created in our dev environment with Windows Server 2022 Datacenter as the OS and PowerBI Server installed. I was able to recreate the issue on that server. It seems to be an issue with Windows Server 2022 Datacenter rejecting the invalid URL.
I also installed .NET Core 6 on the server and the issue remained, so it does not seem to be related to the .NET Framework or .NET Core versions.
I'm going to try to figure out if I can manipulate the URL before PowerBI sends it to the browser so that I can encode the extra '?'. Not sure how to do that yet, if it is even possible.