Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Go to top

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.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace WcfRestService2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            btnLogin.Click += new EventHandler(btnLogin_Click);
            btnLogout.Click += new EventHandler(btnLogout_Click);
            CheckUserIdentity.Click += new EventHandler(CheckUserIdentity_Click);
        }

        void CheckUserIdentity_Click(object sender, EventArgs e)
        {
            FormsIdentity formIdentity = HttpContext.Current.User.Identity as FormsIdentity;
            if (formIdentity != null && formIdentity.IsAuthenticated)
            {
                lblMessage.Text = "User is autheticated";
            }
            else
            {
                lblMessage.Text = "User not authenticated";
            }
        }

        void btnLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            lblMessage.Text = "Signed Out";
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            if (Membership.ValidateUser("admin4", "123456789"))
            {
                FormsAuthentication.SetAuthCookie("admin6", false);
                lblMessage.Text = "Signed In";
                
            }
            
        }
    }
}

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.

License

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

Share

About the Author

Anupama_Agarwal
Architect
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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 19 Mar 2013
Article Copyright 2011 by Anupama_Agarwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid