Monday, July 13, 2020

Checking Anonymous Authentication Allowed on ASP.NET OWIN Middleware and Web Forms

Some business logic on our web application apparently caused issue when hitting a page that allow anonymous authentication. And it seems there is no simple flag that indicates whether a page requires authorization or not.

Problem

I need to check for allow anonymous in ASP.NET Web Forms page and on OWIN middleware.


Solution


For ASP.NET Web Forms page, I found the following thread in StackOverflow which works great:


In my case, I need to convert the code to VB.NET, so it becomes:

Dim principal = New GenericPrincipal(New GenericIdentity(String.Empty, String.Empty), New String() {})
Dim isAllowAnonymous = UrlAuthorizationModule.CheckUrlAccessForPrincipal(Page.AppRelativeVirtualPath, principal, Context.Request.HttpMethod).ToString()

And on OWIN middleware, I need to tweak the above a little bit, so it becomes:

Dim principal = New GenericPrincipal(New GenericIdentity(String.Empty, String.Empty), New String() {})
Dim isAllowAnonymous = UrlAuthorizationModule.CheckUrlAccessForPrincipal(context.Request.Uri.AbsolutePath, principal, context.Request.Method)

No comments:

Post a Comment