Click here to Skip to main content
15,936,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ASP.Net Web Forms application in which I have been tasked with implementing Azure AD authentication.

It seems as if, while debugging the application in localhost, the prompt opens properly - it doesn't do any redirection, but I guess that's an issue with my code or Azure settings.

When I deploy the application on its production server on a third party host (not Azure), the authentication prompt will not open at all.

What could the cause of this be?

Below is the code I use for the authentication process

Imports Microsoft.Identity.Client
Imports Microsoft.VisualBasic

Public Class AuthenticationManager
    Private Shared app As PublicClientApplication

    Shared Sub New()

        Dim clientId As String = ConfigurationManager.AppSettings("ida:ClientId")
        Dim redirectUri As String = ConfigurationManager.AppSettings("ida:RedirectUri")
        Dim tenantId As String = ConfigurationManager.AppSettings("ida:Tenant")
        Dim authorityUri = "" & tenantId
        Dim scopes As String() = New String() {""}

        app = PublicClientApplicationBuilder.Create(clientId).WithAuthority(authorityUri).WithTenantId(tenantId).WithRedirectUri(redirectUri).Build()
    End Sub

    Public Function GetAuthenticationResult() As String
            Dim scopes As String() = New String() {""}

            Dim result As Microsoft.Identity.Client.AuthenticationResult = app.AcquireTokenInteractive(scopes).WithPrompt(Prompt.ForceLogin).ExecuteAsync().Result
            Return result.Account.Username
        Catch ex As Exception
            Return "-"
            Exit Function
        End Try

    End Function
End Class

Private Sub BtnConnectWithAzure_Click(sender As Object, e As EventArgs) Handles BtnConnectWithAzure.Click
        Dim t As New Threading.Thread(AddressOf AuthenticateUser)
    End Sub

    Private Sub AuthenticateUser()
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
        Dim app As New AuthenticationManager
        Dim username As String = app.GetAuthenticationResult()

        If username <> "-" Then
            Response.Redirect(username) 'This could be a problematic part re: redirection            
        End If

    End Sub

What I have tried:

I initially thought this was an issue regarding blocked popups on the browser, but it doesn't seem to be related to that - it doesn't seem to be a permission issue in general. I haven't really found any other suggestions online on what could be causing the issue
Updated 22-May-23 21:34pm

1 solution

AcquireTokenInteractive[^] is used in desktop applications, not websites.

It will pop up a new browser window to prompt for credentials on the machine where the code is running. In this case, that will be the server, where nobody will ever see it. It might appear to work when you debug it; but that's only because, in that specific case, the server and the client are the same machine.

Acquiring a token for a web application requires a different approach:
Get a token in a web app that calls web APIs - Microsoft Entra | Microsoft Learn[^]
Share this answer
Fydobas 23-May-23 3:59am    
Oh that makes complete sense. That link you provided, however, is slightly confusing as it seems to be referring to a Web API/MVC project, not Web Forms like my case - and the samples seem incomplete.
Richard Deeming 23-May-23 5:18am    
Yes, WebForms is essentially "dead" now. You'll start to struggle to find Microsoft documentation even for the core classes, as MS have started "de-listing" the WebForms documentation pages from search engines.

You can find some third-party blog posts that cover integrating MSAL with WebForms - for example:
Create an ASP.NET Web Application (.NET Framework - Web Forms or MVC) using Azure AD Authentication -[^]
Fydobas 23-May-23 6:32am    
I have managed to make most of it work using OWIN. Just that for some reason, I cannot get the user name using HttpContext.Current.GetOwinContext().Authentication.User.Identity.Name... it's always empty.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900