OpenAuth.RequestAuthentication – NullReference Exception Explained
It turns out that setting up OpenAuth for a .NET 4.5 application takes longer than 45seconds for several unlucky people online. This is due to the cryptic “NullReferenceException” in OpenAuthProviders.ascx.cs of the default Web Form template on line “OpenAuth.RequestAuthentication(provider, redirectUrl)”
Most searching online seems to point back to Snuffler71’s question on stackoverflow.com which I did not find to be too useful. After a little bit of searching thru dotPeek I was able to figure this out.
The problem comes from the way the providerName is being passed to OpenAuth.RequestAuthentication. By default the name is registered in lowercase, but the template passes values in camel case (ex: “facebook” vs “Facebook”).
Here is my patch to the template to fix this issue once and for all:
if (IsPostBack)
{
var providerName = Request.Form["provider"];
if (providerName == null)
{
return;
}
var redirectUrl = "~/Account/RegisterExternalLogin.aspx";
if (!String.IsNullOrEmpty(ReturnUrl))
{
var resolvedReturnUrl = ResolveUrl(ReturnUrl);
redirectUrl += "?ReturnUrl=" + HttpUtility
.UrlEncode(resolvedReturnUrl);
}
var provider = OpenAuth.AuthenticationClients
.GetByProviderName(providerName);
if (provider == null)
{
provider = OpenAuth.AuthenticationClients
.GetByProviderName(providerName.ToLowerInvariant());
if (providerName == null)
{
throw new InvalidOperationException(string.Format(
"The provider '{0}' has not been registered with {2}."+
"This is likely an error with AuthConfig.cs settings",
Server.HtmlEncode(providerName),
typeof(OpenAuth).FullName));
}
providerName = providerName.ToLowerInvariant();
}
OpenAuth.RequestAuthentication(providerName, redirectUrl);
}
Interview Question: How Would you Test a Software Component Paper: Being Personally Responsible
One Response to “OpenAuth.RequestAuthentication – NullReference Exception Explained”
Leave a Reply
You must be logged in to post a comment.
You sir are a gent! thank you