Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:13:55+00:00 2026-06-16T15:13:55+00:00

I have created my own Authorize attribute called Authorise… Imports System.Security.Principal <AttributeUsage(AttributeTargets.Method Or AttributeTargets.[Class],

  • 0

I have created my own Authorize attribute called Authorise…

Imports System.Security.Principal

<AttributeUsage(AttributeTargets.Method Or AttributeTargets.[Class], Inherited:=True, AllowMultiple:=True)>
Public Class AuthoriseAttribute
    Inherits AuthorizeAttribute

    Public Overrides Sub OnAuthorization(filterContext As AuthorizationContext)

        Dim CookieName As String = FormsAuthentication.FormsCookieName

        If Not filterContext.HttpContext.User.Identity.IsAuthenticated OrElse filterContext.HttpContext.Request.Cookies Is Nothing OrElse filterContext.HttpContext.Request.Cookies(CookieName) Is Nothing Then
            HandleUnauthorizedRequest(filterContext)
            Return
        End If

        Dim AuthCookie = filterContext.HttpContext.Request.Cookies(CookieName)
        Dim AuthTicket = FormsAuthentication.Decrypt(AuthCookie.Value)
        Dim Roles As String() = AuthTicket.UserData.Split(","c)

        Dim UserIdentity = New GenericIdentity(AuthTicket.Name)
        Dim UserPrincipal = New GenericPrincipal(UserIdentity, Roles)

        filterContext.HttpContext.User = UserPrincipal
        MyBase.OnAuthorization(filterContext)

   End Sub

End Class

I’ve done this so I can use the roles parameter on the attribute, like this…

<Authorise(Roles:="Admin")>

This works perfectly on my pages that require authorisation. However, on my main page, which does not require authorisation (and therefore does not have the Authorise attribute) I would like to display different items depending on whether the user is (a) logged in and (b) whether they are an admin or not. For example…

@If HttpContext.Current.User.Identity.IsAuthenticated Then
    ' Display a welcome message (this works)
    @If HttpContext.Current.User.IsInRole("Admin") Then
        ' Display a settings link (this does not work)
    End If
End If

The “welcome message” part fires but the “settings link” part does not. This makes sense because this view does not have the Authorise attribute.

How can I check the IsInRole on pages that don’t have the Authorise attribute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:13:57+00:00Added an answer on June 16, 2026 at 3:13 pm

    I don’t have a proper solution for this. Just a work around that may help before someone posts the proper solution.

    I use, [Authorize] attribute for the actions but whenever I am in a partial view, I do a manual ‘OnAuthorization’.

    public class Authorize : AuthorizeAttribute
    {
     public override void OnAuthorization(AuthorizationContext filterContext)
     {
       ....
     }
    
     public static void ManualOnAuthorization(HttpContext context)
     {
        if (context.User.Identity.IsAuthenticated && context.User.Identity.AuthenticationType == "Forms")
        {
            FormsIdentity fIdent = (FormsIdentity)context.User.Identity;
            var user = new CustomUser(fIdent.Ticket.UserData);
            var ci = new CustomIdentity(user);
            var p = new CustomPrincipal(ci);
            HttpContext.Current.User = p;
            Thread.CurrentPrincipal = p;
        }
     }
    }
    

    I have put it in Authorize class and use it as following in a partial view.

    @if(User.Identity.IsAuthenticated)
        {
            Authorize.ManualOnAuthorization(HttpContext.Current); 
            if (User.IsInRole("Admin"))
            {
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created my own attribute to decorate my object. [AttributeUsage(AttributeTargets.All)] public class MyCustomAttribute
I have created my own button class called custom_btn. I created it on the
I have my own custom Authorize Attribute and I am trying to check my
I have created my own payment method by taking reference of another method. I
I have created my own Attached Property like this: public static class LabelExtension {
I have created my own customized lots of architecture including n-tier layers for different
I have created my own debugger application. It attaches to a process and creates
I have created blank Asp.Net-MVC 3 web application and want to write my own
I have created a custom list view, each row has it's own custom selector,
I have my own asp.net cookie created like this: var authTicket = new FormsAuthenticationTicket(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.