DooDoo
DooDoo

Reputation: 13447

WCF: How to get requested Web method name in HttpModule and ServiceAuthorizationManager

I want to setting up user authentication and auhorization using 2 methods:

1) HttpModule

2) ServiceAuthorizationManager and override CheckAccessCore

I have a role based authorization and I want to get name of requested web method to do authorization based on web methods.

How I can get requested web method?

Thanks

Upvotes: 1

Views: 614

Answers (1)

rahulaga-msft
rahulaga-msft

Reputation: 4164

I had a similar requirement sometime back and remember vaguely using something like below to serve the purpose :

protected override bool CheckAccessCore(OperationContext operationContext)
        {
            string actionName = GetActionName(operationContext);
            /* do what all further authorization check you want to do
             * like "can user access method with actionname="Create"*/
        }

        private static string GetActionName(OperationContext operationContext)
        {
            string action;

            if (operationContext.RequestContext != null)
            {
                action = operationContext.RequestContext.RequestMessage.Headers.Action;
            }
            else
            {
                action = operationContext.IncomingMessageHeaders.Action;
            }

            if (action == null)// REST Service - webHttpBinding
            {
                action = WebOperationContext.Current.IncomingRequest.UriTemplateMatch == null || WebOperationContext.Current.IncomingRequest.UriTemplateMatch.Data == null
                         ? String.Empty : WebOperationContext.Current.IncomingRequest.UriTemplateMatch.Data.ToString();
            }
            else
            {
                action = action.Split('/').Last();
            }
            return action;
        }

You can tweak above code snippet to better closely meet your requirement but for sure this will give you clear picture on how to extract method name.

Upvotes: 2

Related Questions