Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have successfully implemented authentication via Azure on my ASP.Net Web Forms website/application using the OWIN module. However, when I try to get the authenticated user's identity, it seems to display/return an empty string

Here is my authentication code

          System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
          Dim redirectUri As String = ConfigurationManager.AppSettings("ida:RedirectUri")
          Dim authProperties As New AuthenticationProperties
          authProperties.RedirectUri = redirectUri
          HttpContext.Current.GetOwinContext().Authentication.Challenge(authProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType)
      Catch ex As Exception
          LblErrorMsg.Text = ex.Message
          PnlError.Visible = True
          Exit Sub
      End Try

and here's my Startup.vb class

Imports Microsoft.Owin.Security.OpenIdConnect
Imports Microsoft.Owin.Security.Notifications
Imports System.Threading.Tasks
Imports System
Imports Microsoft.Owin.Host.SystemWeb

<Assembly: OwinStartup("MyStartupClass", GetType(Startup))>
Public Class Startup
    Private clientId As String = System.Configuration.ConfigurationManager.AppSettings("ida:ClientId")
    Private redirectUri As String = System.Configuration.ConfigurationManager.AppSettings("ida:RedirectUri")
    Shared tenant As String = System.Configuration.ConfigurationManager.AppSettings("ida:Tenant")
    Private authority As String = String.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings("ida:Authority"), tenant)

    Public Sub Configuration(ByVal app As IAppBuilder)
        app.UseCookieAuthentication(New CookieAuthenticationOptions With {
            .CookieManager = New SystemWebCookieManager()
        app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions With {
            .ClientId = clientId,
            .Authority = authority,
            .RedirectUri = redirectUri,
            .PostLogoutRedirectUri = redirectUri,
            .Scope = OpenIdConnectScope.OpenIdProfile,
            .ResponseType = OpenIdConnectResponseType.IdToken,
            .TokenValidationParameters = New TokenValidationParameters() With {
                .ValidateIssuer = False
            .Notifications = New OpenIdConnectAuthenticationNotifications With {
                .AuthenticationFailed = AddressOf OnAuthenticationFailed
    End Sub

    Private Function OnAuthenticationFailed(ByVal context As AuthenticationFailedNotification(Of OpenIdConnectMessage, OpenIdConnectAuthenticationOptions)) As Task
        context.Response.Redirect("/?errormessage=" & context.Exception.Message)
        Return Task.FromResult(0)
    End Function
End Class

Here is the code I am using to get the signed in user on the dashboard page

Dim name As String = ""

       If HttpContext.Current.GetOwinContext().Authentication.User.Identity.IsAuthenticated = True Then
           name = HttpContext.Current.GetOwinContext().Authentication.User.Identity.Name
       End If

What I have tried:

I have tried disabling Windows authentication in the web.config but the user name is still an empty string
Updated 23-May-23 4:10am

I don't know what the root cause of the problem was, but I've figured out an alternative way: using the Claims object exposed by OWIN to get the username.

If HttpContext.Current.GetOwinContext().Authentication.User.Identity.IsAuthenticated = True Then
           name = HttpContext.Current.GetOwinContext().Authentication.User.Claims.Where(Function(x) x.Type = "preferred_username").Single.Value.ToString
End If
Share this answer
Andre Oosthuizen 23-May-23 11:34am    
Glad you found the solution - +5.
Microsoft docs and using Owin suggest to check some of the following, I trust the answer might lie in here -

1. Check that the necessary NuGet packages are installed correctly and is available for OWIN and Azure AD authentication.

2. Check the Azure AD configuration in the Web.config file. Verify that the values for ida:ClientId, ida:RedirectUri, ida:Tenant, and ida:Authority are correctly configured in the appSettings section of your Web.config file.

3. Check that you have registered the correct redirect URIs in your Azure AD app registration. The redirect URI configured in the Azure AD app registration should match the ida:RedirectUri value specified in the Web.config file.

4. Check that the necessary middleware is configured correctly in the Startup.vb file at app.UseCookieAuthentication and app.UseOpenIdConnectAuthentication configurations.

5. Check that you have the appropriate permissions configured in your Azure AD app registration. The configured permissions should match the OpenIdConnectScope property of the OpenIdConnectAuthenticationOptions.
Share this answer

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