Click here to Skip to main content
13,046,087 members (46,077 online)
Click here to Skip to main content

Tagged as


49 bookmarked
Posted 25 Dec 2011

WCF REST 4.0 Authorization with Form Based Authentication (SetAuthCookie)

, 19 Mar 2013
How to create custom authorization policy and return HTTPContext Identity for authorization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Security.Permissions;

namespace WcfRestService2
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class Service1
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [WebGet(UriTemplate = "All")]
        [PrincipalPermission(SecurityAction.Demand, Role="Admin")]
        [PrincipalPermission(SecurityAction.Demand, Role = "Finance")]
        public List<SampleItem> GetCollection()
            // TODO: Replace the current implementation to return a collection of SampleItem instances
            var value =  System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
            return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };

        [WebInvoke(UriTemplate = "", Method = "POST")]
        public SampleItem Create(SampleItem instance)
            // TODO: Add the new instance of SampleItem to the collection
            throw new NotImplementedException();

        [WebGet(UriTemplate = "{id}")]
        public SampleItem Get(string id)
            // TODO: Return the instance of SampleItem with the given id
            throw new NotImplementedException();

        [WebInvoke(UriTemplate = "{id}", Method = "PUT")]
        public SampleItem Update(string id, SampleItem instance)
            // TODO: Update the given instance of SampleItem in the collection
            throw new NotImplementedException();

        [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public void Delete(string id)
            // TODO: Remove the instance of SampleItem with the given id from the collection
            throw new NotImplementedException();


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
I work as a freelance consultant and is passionate about taking challenges in latest technology.
I am a solution architect and trainer with 9+ years experience in designing, developing and maintaining enterprise wide application using latest technology like SharePoint 2010, MOSS 2007, Business Intelligence, SQL Server 2008, Reporting Service, Analysis Service and Integration service.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 19 Mar 2013
Article Copyright 2011 by Anupama_Agarwal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid