0

ユーザーに固有のさまざまな要因に基づいて特定のコントローラーとリソースへのアクセスを制限するAuthorizeAttributeを継承するCustomAuthorizeというカスタム認証プログラムがあります。ただし、次の行でエラーが発生します。

この線:

保護されたオーバーライド関数AuthorizeCore(httpContext As HttpContextBase)As Boolean

エラー:

タイプ'System.StackOverflowException'の未処理の例外がMyBlog.DLLで発生しました

これが私のコード全体です:

パブリッククラスCustomAuthorizeはAuthorizeAttributeを継承します

Protected Overrides Function AuthorizeCore(httpContext As HttpContextBase) As Boolean

    Dim authorized = AuthorizeCore(httpContext)

    ' if user is not authorized, restrict access
    If (authorized = False) Then

        Return False

    End If

    ' get user name
    Dim username = httpContext.User.Identity.Name

    ' get user
    Dim user = Membership.GetUser(username, True)

    ' get user's profile
    Dim db As UserProfileDbContext = New UserProfileDbContext
    Dim profile = db.UserProfiles.Where(Function(x) x.UserId = user.ProviderUserKey).Single

    ' TODO: if user doesn't have a profile, return false

    ' get route
    Dim routeData = httpContext.Request.RequestContext.RouteData

    ' get controller
    Dim controller = routeData.Values("controller").ToString

    ' get id
    Dim id = routeData.Values("id").ToString

    ' if no id is set, check to see if the user owns the requested entity (company or blog)
    If String.IsNullOrEmpty(id) = True Then

        If controller.ToLower = "blog" Or controller.ToLower = "article" Then

            If profile.IsCompanyOwner Or profile.IsBlogOwner = True Then

                ' if user is owner of a blog with no specified id, then it will default to their own blog
                Return True

            End If

        End If

    Else

        ' if controller = blog
        '       check for blog id

        If controller.ToLower = "blog" Then

            ' check to see if the user owns the company to which the blog belongs
            If profile.IsCompanyOwner Then

                ' get company from blog id
                Dim db1 As BlogDbContext = New BlogDbContext
                Dim blog = db1.Blogs.Where(Function(b) b.BlogId = id).Single()

                If blog.CompanyId = profile.CompanyId Then

                    Return True

                End If

            ElseIf profile.IsBlogOwner Then

                ' if user's blog id is the blog being requested, grant access
                If profile.BlogId = id Then

                    Return True

                End If

            End If

        End If

        ' if controller = article
        '       check for article blog id

        If controller.ToLower = "article" Then

            Dim db2 As ArticleDbContext = New ArticleDbContext
            Dim article = db2.Articles.Where(Function(a) a.ArticleId = id).Single
            Dim articleBlogId = article.BlogId

            ' check to see if the user owns the company to which the blog belongs
            If profile.IsCompanyOwner Then

                ' get company from blog id
                Dim db1 As BlogDbContext = New BlogDbContext
                Dim blog = db1.Blogs.Where(Function(b) b.BlogId = articleBlogId).Single()

                If blog.CompanyId = profile.CompanyId Then

                    Return True

                End If

            ElseIf profile.IsBlogOwner Then

                ' if user's blog id is the blog being requested, grant access
                If profile.BlogId = articleBlogId Then

                    Return True

                End If

            End If

        End If

    End If

    ' if we got this far, then the user shouldn't have access
    Return False

End Function

Protected Overrides Sub HandleUnauthorizedRequest(filterContext As AuthorizationContext)
    Dim result = New ViewResult()
    result.ViewName = "Error"
    result.ViewBag.ErrorMessage = "oops, you are not allowed"
    filterContext.Result = result
End Sub

エンドクラス

このエラーを修正するにはどうすればよいですか?ありがとうございました。

4

2 に答える 2

1

と呼びたいと思いますMyBase.AuthorizeCore

したがって、この行を変更します

Dim authorized = AuthorizeCore(httpContext)

Dim authorized = MyBase.AuthorizeCore(httpContext)
于 2012-08-07T13:40:52.263 に答える
1

関数の最初の行はDim authorized = AuthorizeCore(httpContext)

この行はメソッドを再度呼び出し、その新しい呼び出しの最初の行は同じことを無限に行います。これにより、が発生しStackOverflowExceptionます。

于 2012-08-07T13:42:00.080 に答える