Tips and tricks for .NET using ASP and VB code.

Force Windows authentication and get username

    Public PublicCurrentUserWindowsIdentityName As String = "Unknown"
    Public PublicEnvironmentUserName As String = "Unknown"

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim WebConfiguration As System.Configuration.Configuration = Web.Configuration.WebConfigurationManager.OpenWebConfiguration("")
        Dim WebConfigurationAuthenticationSection As Web.Configuration.AuthenticationSection = CType(WebConfiguration.GetSection("system.web/authentication"), Web.Configuration.AuthenticationSection)
        WebConfigurationAuthenticationSection.Mode = Web.Configuration.AuthenticationMode.Windows
        Dim CurrentUserIdentity As System.Security.Principal.IIdentity = HttpContext.Current.User.Identity
        Dim CurrentUserWindowsIdentity As System.Security.Principal.WindowsIdentity = DirectCast(CurrentUserIdentity, System.Security.Principal.WindowsIdentity)
        PublicCurrentUserWindowsIdentityName = CurrentUserWindowsIdentity.Name
        PublicEnvironmentUserName = Environment.UserName
    End Sub

No comments:

Post a Comment

Search This Blog