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 4330722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:00:06+00:00 2026-05-21T10:00:06+00:00

Related to this question , I’ve got a custom UserNamePasswordValidator that logs in to

  • 0

Related to this question, I’ve got a custom UserNamePasswordValidator that logs in to our internal API. As part of this logging-in, I can discover the user’s roles in our system.

I’d like to later use these in PrincipalPermissionAttribute demands on the service methods, e.g.:

[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role = "System Administrator")]
public string HelloWorld()
{ /* ... */ }
  • 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-05-21T10:00:06+00:00Added an answer on May 21, 2026 at 10:00 am

    To expand on Ladislav’s answer:

    No. A custom UserNamePasswordValidator cannot be used as a role provider. The UserNamePasswordValidator runs in a separate context (or thread, or something) from the OperationContext that you want to mess with.

    What you need to do instead is implement custom authorization. I found this page most useful for doing this. Warning: there’s a lot of plumbing before you get to the interesting bits.

    Essentially, you start with a ServiceCredentials-derived class, registered in App.config, as follows:

    <serviceBehaviors>
      <behavior name="...">
        <serviceAuthorization principalPermissionMode="Custom" />
    
        <serviceCredentials type="MyNamespace.MyServiceCredentials, MyAssembly">
          <userNameAuthentication userNamePasswordValidationMode="Custom" />
    
          <serviceCertificate etc. />
        </serviceCredentials>
    

    Associate the behavior with your service.

    Override ServiceCredentials.CreateSecurityTokenManager to return a MySecurityTokenManager, derived from ServiceCredentialsSecurityTokenManager. On that, override CreateSecurityTokenAuthenticator, returning a MySecurityTokenAuthenticator. That should be derived from CustomUserNameSecurityTokenAuthenticator. In that, override ValidateUserNamePasswordCore. Call the base class, which will return a list of authorization policies.

    To that list, add a new one: MyAuthorizationPolicy, which implements IAuthorizationPolicy. In that, you merely (hah) need to do the following:

    public bool Evaluate(EvaluationContext evaluationContext, ref object state)
    {
        IList<IIdentity> identities = GetIdentities(evaluationContext);
    
        // Find the GenericIdentity with our user-name in it.
        IIdentity currentIdentity = identities.SingleOrDefault(
            i => i is GenericIdentity &&
            StringComparer.OrdinalIgnoreCase.Equals(i.Name, UserName));
        if (currentIdentity == null)
            throw new InvalidOperationException("No Identity found");
    
        // Replace the GenericIdentity with a new one.
        identities.Remove(currentIdentity);
        var newIdentity =
            new GenericIdentity(_userName, currentIdentity.AuthenticationType);
        identities.Add(newIdentity);
    
        // This makes it available as
        // ServiceSecurityContext.Current.PrimaryIdentity later.
        evaluationContext.Properties["PrimaryIdentity"] = newIdentity;
    
        // This makes it available as Thread.CurrentPrincipal.
        IPrincipal newPrincipal = new GenericPrincipal(newIdentity, _roles);
        evaluationContext.Properties["Principal"] = newPrincipal;
    
        return true;
    }
    
    private static IList<IIdentity> GetIdentities(
        EvaluationContext evaluationContext)
    {
        object identitiesProperty;
        if (!evaluationContext.Properties.TryGetValue(
            "Identities", out identitiesProperty))
        throw new InvalidOperationException("No Identity found");
    
        var identities = identitiesProperty as IList<IIdentity>;
        if (identities == null)
            throw new InvalidOperationException("No Identity found");
        return identities;
    }
    

    And then, having done that lot, you can mark up your service operations with PrincipalPermission:

    [PrincipalPermission(SecurityAction.Demand, Role = "Editor")]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Related to this question , I'm instantiating a connection to our internal API inside
Related to this question , is the idea of a default servlet that serves
My question is related to this question: Baseline snaplines in custom Winforms controls However,
Related to this question here. Can I check if an element in the DOM
Related to this question: On postback, how can I check which control cause postback
This question is related to this post but I don't see how I can
Related to this question , how do I detect that a form user is
Looking through PIL (and related to this question ), where can I get a
Related to this question, can / should I use jar signing to create a
This is directly related to this question I finally figured out that Rails 3.0.6

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.