VB.NET VS2017 net 4.6.1 moving AspNetIdentity 2.2.2 code from website project to reusable DLL since I have 80+ web sites to convert

21 views Asked by At

Using package Microsoft.AspNet.Identity.Core.2.2.2\lib\net45\Microsoft.AspNet.Identity.Core.dll

The following IntranetSecurity.vb code works when called within the website code base. Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)() returned an ApplicationIserManager like it should.

But when I moved my IntranetSecurity.vb class code module into a class library DLL and called it from my website, the code returns nothing. Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)() returns nothing

Each of our webpages that required security rights start with this code on the webpage Page_Load

**Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim SEC As New IntranetSecurity(Page, "Add Intranet User")**

**HOW CAN I REFERENCE THE ApplicationUserManager OF THE CALLING CODE??? **

Public Class IntranetSecurity
    Inherits Page
    Implements IDisposable

    Public Sub New(ByRef p As Page, ByVal ApplicationName As String)
        'Used by every web page within 'Your Options' menu
        pgMain = p
        LoadAuthenticatedUser(p)
        If IsValidUser Then
            If Not HasAccessToApplication(ApplicationName) Then
                'Authenticated User has No right to access ApplicationName value
                p.Response.Redirect("AccessDenied.aspx")
            Else
                'Authenticated User has right to access ApplicationName value
                Dim i As Int16 = 0  'For debug purposes
            End If
        Else
            'Not a valid intranet user so they can't possibly
            'have any rights to anything...so deny access by default.
            p.Response.Redirect("AccessDenied.aspx")
        End If
    End Sub

    Private Sub LoadAuthenticatedUser(ByRef p As Page)
        pgMain = p
        ' Verify the current user is Authenticated
        _AspNetUserId = User.Identity.GetUserId()
        _UserName = User.Identity.GetUserName()
        _IsValidUser = User.Identity.IsAuthenticated
        If IsValidUser Then
            **Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()**
            'Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
            Dim appUser = manager.FindById(User.Identity.GetUserId())
            ' Load web page variables and properties for the Authenticated pplicationUser
            _UserId = appUser.UserId
            _UserName = appUser.UserName
            _UserPartName = appUser.UserPartName
            _FirstName = appUser.FirstName
            _MiddleName = appUser.MiddleName
            _LastName = appUser.LastName
            _FullName = appUser.FullName
            _EmployeeNumber = appUser.EmployeeNum
            _CostCenter = appUser.CostCenter
            _PrimaryDept = appUser.PrimaryDept
            _IsGeneric = appUser.IsGeneric
            _RolesList = ApplicationList(manager)
        Else
            ' Clear web page variables and properties for the NONE Authenticated ApplicationUser
            _UserId = String.Empty
            _UserName = String.Empty
            _UserPartName = String.Empty
            _FirstName = String.Empty
            _MiddleName = String.Empty
            _LastName = String.Empty
            _FullName = String.Empty
            _EmployeeNumber = String.Empty
            _CostCenter = String.Empty
            _PrimaryDept = String.Empty
            _IsGeneric = String.Empty
            _RolesList = String.Empty
        End If
    End Sub

    Public Function HasAccessToApplication(ByVal ApplicationName As String) As Boolean
        ' This code verifies the Authenticateds User has rights to the ApplicationName
        Dim intFoundAtPosition As Integer = RolesList.IndexOf(ApplicationName)
        If intFoundAtPosition = -1 Then
            Return False
        Else
            Return True
        End If
    End Function

    Public Overrides Sub Dispose() Implements IDisposable.Dispose
    End Sub
End Class

I have tried a variety of ways to get this functionality to work from a DLL class method. Everything returned nothing. I want to keep the interface the same to minimize the impact on the existing code base which is 80+ website applications. I even gave up on the interface impact just to get something working and attempted to pass the ApplicationUserManager object byref to a new New method, but I could not get the syntax correct.

Environment Win10, VB.net VS2017, .Net framework 4.6.1, ASP.Net webforms, Using packages Microsoft.AspNet.Identity.Core.2.2.2\lib\net45\Microsoft.AspNet.Identity.Core.dll Microsoft.AspNet.Identity.Owin.2.2.2\lib\net45\Microsoft.AspNet.Identity.Owin.dll Microsoft.Owin.4.0.0\lib\net451\Microsoft.Owin.dll EntityFramework.6.2.0\lib\net45\EntityFramework.dll

0

There are 0 answers