Sharing Authentication, Profile and Role data between Web Applications and Windows Forms applications using the ASP.NET Application Services

Published Thursday, December 11, 2008 1:16 AM

Some of us might have used the ASP.NET Authentication, Profile or Roles services that shipped with the .Net Framework 3.5. Another advantage of these services is their integration with windows forms applications and the fact that user authentication, role and profile data can be shared between windows forms and web applications.

Scenario: Lets say a company “Contoso” has a few in-house applications, some built as web applications and some as Windows Forms applications. Contoso wants to have user information along with their roles stored at one location and wants all of its applications to use the same store to authenticate users as well as authorize requests for various resources based on their roles. Contoso also wants the user settings / preferences to be shared between all these applications. This can quite easily be accomplished by using the inbuilt ASP.NET Application Services. The Web Applications can be setup to use the ASP.NET Authentication, Roles and Profile services . The Windows Forms applications can be setup to use the ASP.NET Application Services from one of those web applications. Here's a walk-through.

Step A- Create the online ASP.NET Application Services

1. Open Visual Studio 2008 and create a website targeting the .Net Framework version 3.5. Lets call this website “AppServices”

2. Let’s turn on the Authentication, Profile and Roles services –

To do this all we simply need to do is add the following to the web.config file of the AppServices website. "prop1" and "prop2" are the profile properties we'll expose further below.

<system.web.extensions>
        <scripting>
            <webServices>
                <authenticationService enabled="true"/>
                <roleService enabled="true"/>
                <profileService enabled="true" readAccessProperties="prop1,prop2" 
                        writeAccessProperties="prop1,prop2"/>
            </webServices>
        </scripting>
    </system.web.extensions>

By default, this causes the default Membership, Profile and Role providers to be used, which in turn use SqlExpress (by default) to create and access the database. SQL server can also be used. These providers can be changed to go against any database and are also explained in other articles.

3. Let’s use Forms Authentication for the purposes of this sample. So set the following into the web.config file:

<authentication mode="Forms" />

4. We’ll need to enable the RoleManager, so lets add the following to the web.config file under the “<system.web>” section:

<roleManager enabled="true"></roleManager>

5. Lets enable some profile properties for use in a windows forms application, so lets add the following to the web.config file under the “<system.web>” section:

<profile enabled="true">
    <properties>
        <add name="prop1" type="System.String"/>
        <add name="prop2" type="System.String"/>
    </properties>
</profile>

6. Let’s now setup some users and Roles. This can be done through a fancier UI, but for now, let’s just do it through code. We’ll create a page called “CreateUsersAndRoles.aspx” inside this website and let’s copy the following code into its Page_Load method.

protected void Page_Load(object o, EventArgs e)
    {
        //Lets create two roles
        Roles.CreateRole("Admin");
        Roles.CreateRole("User");

        //Lets also create two users
        MembershipCreateStatus status;
        MembershipUser user1 = Membership.CreateUser("user1", "Password@1", 
            "someemail@email.com", "question", "answer", true, out status);
        MembershipUser user2 = Membership.CreateUser("user2", "Password@1", 
            "someemail@email.com", "question", "answer", true, out status);

        // Now let's assign these users to their roles
        Roles.AddUserToRole("user1", "Admin");
        Roles.AddUserToRole("user2", "User");
    }

The Application Services are now setup.

Step B - Create the Windows Forms Application

1. Through Visual Studio 2008, create a new Windows Forms Application (Lets call it MyWinFormsApp) targeting the .Net Framework version 3.5

2. Go to the project properties, and in the services tab, check the "Enable client application services" checkbox. Select "Use Forms Authentication". Also set the service locations as below ("http://localhost/AppServices/" in this case).

set_services 

3. On  the Settings tab, click the "Load Web Settings" button. Authenticate as a user in the database ("user1", "Password@1" in this case). Click the "login" button. You should see something like:

load_web_settings

Step C. And we're done!

To use authentication, profiles and roles, a small example is:

if (!Membership.ValidateUser("user1", "Password@1"))
{
    MessageBox.Show("Unable to authenticate.", "Not logged in",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
    Application.Exit();
}
else
{
    IIdentity user = System.Threading.Thread.CurrentPrincipal.Identity;
    MessageBox.Show("Logged in user is " + 
    user.Name);
    MessageBox.Show("Is user in role: " + 
        Roles.IsUserInRole(user.Name,"Admin"));
    Properties.Settings.Default.prop1 = "foo";
    Properties.Settings.Default.prop2 = "bar";
    MessageBox.Show(Properties.Settings.Default.prop1 + 
        Properties.Settings.Default.prop2);
}

Note: There are also tutorials on the internet that explain how to use "Client Application Services" in Windows Forms Applications even in offline / disconnected mode. Note that the users are being authenticated, or profile data being accessed through calls to the application services hosted through the web application.

Carl Dacosta
ASP.NET QA Team

Comments

# Web Development Community said on Thursday, December 11, 2008 5:06 AM

You are voted (great) - Trackback from Web Development Community

# WebDevVote said on Thursday, December 11, 2008 5:08 AM

you are voted!

Track back from www.webdevvote.com/.../Sharing_Authentication_Profile_and_Role_data_between_Web_Applications

# rascunho » Blog Archive » links for 2008-12-11 said on Thursday, December 11, 2008 3:10 PM

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2008-12-11

# Tech Guru said on Friday, December 12, 2008 10:24 PM

Share ASP.NET Authentication Between Other Applications

# name (required) said on Saturday, December 13, 2008 4:07 AM

awesome, i had no idea this was integrated in 2k8. you just saved me a bunch of time :)

# IrishChieft said on Sunday, December 14, 2008 10:04 AM

Great article! Some questions: What about the situation where we have many Web applications, both 1.1 and 2.0, using roles and permissions? Are there some best practices for integration where one also has active directory in the mix? Also, if we are talking about Web Application projects, profiles are not supported, right?

Anthony :-)

# hotel t�rkei said on Monday, January 25, 2010 6:01 PM

Variation Opportunity,unlikely teaching heat damage capacity full perform occasion destroy collection himself official room bedroom upper arrangement reveal energy drink interest visit investigate yesterday wage count blue potential outcome all candidate remove stick brother other partner sea teacher clothes drink best define asset towards estate growth total other nuclear teach blood district attempt beat works finance smile establishment investigate museum check component argue significant magazine stand commercial less crime kitchen understanding energy go out sheet notice both wonder might sleep introduce prevent relative suggestion require buy category hear world

# developer said on Tuesday, March 23, 2010 8:31 PM

Hi, i have to create a WinForm Aplication to Admin users in the sqlmembership provider, and i have some cuestions 1. Is secure use this methond in extranet? 2. what is more secure install the aspnetdb in militarized zone or desmilitarized zone? 3. if not secure, is necesary create a WebService in desmilitarized zone that public methods to create users? thanks!!

my email is testfordownloads001@hotmail.com

# Davidoff Cigars said on Wednesday, December 15, 2010 5:52 AM

I am glad you said that??

<a href="http://davidoff.corecommerce.com">Davidoff</a>

# Andre Stanford said on Thursday, December 23, 2010 12:18 AM

FAIL :)

<a href="http://alternativemedicine.org.in">alternative medicine courses in India</a>

# Brandie said on Thursday, December 23, 2010 5:57 PM

I am glad you said that :P

<a href="http://alternativemedicine.org.in">alternative medicine institutes</a>

# Tami Stallings said on Friday, December 24, 2010 4:53 PM

Hey , WTF??

<a href="http://webreputationmanagement.info">corporate reputation management</a>

# Antoinette said on Saturday, December 25, 2010 12:39 AM

Great writing! Maybe you could do a follow up to this topic :D

-Thanks,

<a href="www.live-girls-webcam-chat.com/">chat bayern</a>

# German said on Saturday, December 25, 2010 6:23 PM

FTW!?

<a href="www.live-girls-webcam-chat.com/">chat community</a>

# Mohammed Bailey said on Sunday, December 26, 2010 1:51 PM

Hey , whatever dude.

<a href="http://www.findgroomers.com">dog grooming</a>

# Andy said on Monday, December 27, 2010 4:13 PM

I'm glad you said that :P

<a href="www.live-girls-webcam-chat.com/webcam-chat-girls.html">hamburg chat</a>

# Lynn Nicholas said on Tuesday, December 28, 2010 3:53 PM

I'm glad you wrote this post!!!

<a href="http://www.findgroomers.com">dog grooming</a>

# Quinn said on Wednesday, December 29, 2010 2:16 PM

I am curious just what  thinks with that!!!

<a href="alternativemedicine.org.in/massage_therapy.html">massage therapy courses</a>

# Annabelle Gore said on Friday, December 31, 2010 3:18 PM

Maybe the most amazing page that I have read this week!?

<a href="alternativemedicinecourse.com/.../naturopathy-courses">naturopathy courses in India</a>

# Florine Locklear said on Saturday, January 01, 2011 1:22 PM

I'm very happy that you wrote this post!

<a href="alternativemedicine.org.in/osteopathy.html">osteopathy institute</a>

# Kermit Colon said on Sunday, January 02, 2011 2:39 PM

Terri is the best!?

<a href="www.live-girls-webcam-chat.com/webmaster.html">blog geld verdienen</a>

# Arturo said on Monday, January 03, 2011 5:55 AM

Great post! You might want to follow up to this topic!?!

<a href="http://www.cigars-now.com">Buy cigars</a>

# Coleen said on Tuesday, January 04, 2011 4:54 AM

I need to know  what Marlon thinks about that :P

Molly

<a href="www.lida-schlankheitskapseln.com/">lida diät</a>

# Gino said on Wednesday, January 05, 2011 4:46 PM

Great writing! Maybe you could do a follow up to this topic..

Kindest regards

Amie

<a href="http://www.panicawayreview.net">panic away</a>

# Chauncey said on Thursday, January 06, 2011 5:29 PM

Hey Liza, I doubt it!!

Tia

<a href="www.erotiklobby.com/">erotik bilder voting</a>

# Constance said on Friday, January 07, 2011 12:53 PM

I am curious  what Maxwell will say about that!!

Christopher

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Celia Gomes said on Saturday, January 08, 2011 6:29 AM

I am wondering  what Grady says about that?

Christi

<a href="http://www.SecurityCubed.com">home security systems</a>

# Jarrod said on Saturday, January 08, 2011 3:43 PM

Great writing! I want to see a follow up to this topic???

Thanks,

Alphonso

<a href="fickmaschine-live.com/">live dildofick</a>

# Hallie said on Sunday, January 09, 2011 11:18 AM

Could be the most influential paper that I read this month??

<a href="fickmaschine-live.com/.../a>

# Cary Carr said on Monday, January 10, 2011 3:21 PM

Great writing, I've been waiting for that?!?

Bernard

<a href="http://webreputationmanagement.info">brand reputation management</a>

# Burt said on Tuesday, January 11, 2011 5:30 AM

Casandra, really...

Thurman

<a href="www.ipcounter.net/.../a>

# Rory Humphrey said on Tuesday, January 11, 2011 4:20 PM

I'm glad you said that!!!

<a href="www.gbbilder4you.com/">gb bilder indianer</a>

# Dale said on Wednesday, January 12, 2011 2:03 PM

I'm very thrilled you took the time and wrote that post!!!

<a href="http://www.asparagus-soap.com">Natural Soap</a>

# Rocky Reilly said on Wednesday, January 12, 2011 6:46 PM

I want to know just what Landon thinks with that..

<a href="http://webreputationmanagement.info">my web site</a>

# Rosa said on Thursday, January 13, 2011 4:41 PM

The most amazing paper that I have read this month?!

My regards,

Alice

<a href="mizarstvo-jereb.si/.../">previjalna miza</a>

# Colette Muller said on Monday, January 17, 2011 5:25 PM

I want to know just what Claude thinks with this??

<a href="http://alternativemedicinecourse.com">alternative medicine course</a>

# Lenora said on Tuesday, January 18, 2011 9:44 AM

Great post! I wish you could follow up to this topic :P

<a href="alternativemedicinecourse.com/.../aromatherapy-courses">aromatherapy course</a>

# Sandra Palmer said on Tuesday, January 18, 2011 6:26 PM

Hey Henrietta, rofl???

Caroline

<a href="www.cigars-now.com/.../oliva-cigars.html">oliva cigar</a>

# Lourdes said on Wednesday, January 19, 2011 4:09 PM

Great post! I want to see a follow up on this topic...

<a href="www.asparagus-soap.com/.../scrubs.html">Oatmeal scrubs</a>

# Earline Diamond said on Wednesday, January 19, 2011 9:55 PM

Great writing, I've been after that!!

Lisa

<a href="alternativemedicinecourse.com/.../massage-therapy-courses">massage therapy course</a>

# Jeanne Sapp said on Thursday, January 20, 2011 7:41 PM

Great read! You may want to follow up on this topic.

-Sincere Regards,

Ross

<a href="www.parketarstvo-cerkvenik.si/.../a>

# Trinidad Jones said on Friday, January 21, 2011 5:25 PM

Glenn FAIL :P

Ricardo

<a href="alternativemedicinecourse.com/.../magneto-therapy-courses">magneto therapy course</a>

# Earlene Suarez said on Saturday, January 22, 2011 12:05 AM

Yvette, who cares.

Joanna

<a href="en.netlog.com/.../blogid=4005913">Belanja Baju Online</a>

# Sharlene said on Saturday, January 22, 2011 7:51 PM

I need to know exactly what Clayton can do about this??

<a href="wiki.cio.ny.gov/.../User_talk:Yanagisawa">Toko Baju</a>

# Phyllis said on Sunday, January 23, 2011 5:35 PM

This is the BEST topic that I read this year :P

-Regards

Trevor

<a href="www.oregonlngpropertysearch.com/">moncler jacket</a>

# Carlton said on Sunday, January 23, 2011 10:00 PM

This is the most amazing read I read this year??

<a href="data-recovery-information.com/">Data recovery</a>

# Erick Connelly said on Monday, January 24, 2011 5:30 PM

I'm very happy you wrote this???

<a href="www.oregonlngpropertysearch.com/">moncler jacket</a>

# Adrian Watts said on Tuesday, January 25, 2011 3:20 PM

I'm glad you said that :P

-Yours Truly,

Bobbi

<a href="mizarstvo-jereb.si/.../">previjalna miza</a>

# Alfonzo Alonso said on Thursday, January 27, 2011 1:35 PM

Thad ftw :P

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Aubrey said on Thursday, January 27, 2011 6:04 PM

Hey Hallie, I doubt it?!

<a href="http://www.gume-oblak.si">gume</a>

# Leopoldo said on Friday, January 28, 2011 4:55 PM

Maybe the most interesting topic I read all year.

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Marla Cates said on Saturday, January 29, 2011 5:53 PM

Great read! Maybe you could do a follow up on this topic!!

Warmest regards

Carlene

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Connie Riley said on Tuesday, February 01, 2011 10:43 PM

Great writing! I want you to follow up on this topic?

Maurice

<a href="www.cigars-now.com/.../a>

# Nettie said on Wednesday, February 02, 2011 9:37 PM

I am curious  what Jay will change about that!!!

Coleman

<a href="http://www.yutube.si">yutube</a>

# Blanche Rubin said on Thursday, February 03, 2011 6:53 PM

I have to hear  what Celeste will do about that :P

Thank You

Harry

<a href="www.camchatladies.com/">freunde chat</a>

# Julia said on Friday, February 04, 2011 5:37 PM

The most amazing read that I have read all year!?

Bryan

<a href="http://url.com">keyword one</a>

# Michel said on Sunday, February 06, 2011 6:05 AM

Hey Leona, and pigs fly!?!

Mitchell

<a href="www.sumobulldogs.com/">English bulldog</a>

# Kris Garrison said on Monday, February 07, 2011 3:51 AM

Could be the top topic that I read this month??

Chi

<a href="www.sumobulldogs.com/english_bulldog_pups_for_sale.html">english bulldog pups for sale</a>

# Odell said on Tuesday, February 08, 2011 3:29 PM

I'm glad you said that..

<a href="www.was-frauen-wollen.com/">eigenes sperma probieren</a>

# Leo said on Wednesday, February 09, 2011 1:12 PM

Landon ROCKS???

Alberto

<a href="http://www.yutube.si">yutube</a>

# Quincy said on Wednesday, February 09, 2011 6:57 PM

I have to hear just what Trevor will do about this!

-Sincere regards

Francisco

<a href="www.kuhinje-nokturno.si/.../a>

# Tamra said on Thursday, February 10, 2011 5:39 PM

Janna rocks :)

<a href="http://www.yutube.si">yutube</a>

# Nichole said on Friday, February 11, 2011 3:22 PM

Hey Tracey, lol?

<a href="http://www.zlatorogi.si">ansambel</a>

# Josephine said on Saturday, February 12, 2011 1:47 AM

I am glad you said that!

<a href="http://url.com">keyword one</a>

# Lindsey said on Sunday, February 13, 2011 2:33 PM

I need to know just what Frances will say about this?

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Savannah said on Sunday, February 13, 2011 7:38 PM

I wonder just what Rico thinks with that?!?

<a href="www.was-frauen-wollen.com/">sperma abpumpen</a>

# Jerry said on Monday, February 14, 2011 5:20 PM

Barbara fail??

<a href="data-recovery-information.com/hard-drive-data-recovery.html">Hard drive data recovery</a>

# Fran said on Monday, February 14, 2011 11:57 PM

Great read! I want to see a follow up on this topic!

Tyson

<a href="www.was-frauen-wollen.com/">rotes sperma</a>

# Gonzalo Blum said on Tuesday, February 15, 2011 8:00 PM

Kip is the best??

<a href="www.was-frauen-wollen.com/">sperma im alter</a>

# Pearlie Lester said on Wednesday, February 16, 2011 5:43 PM

I am pleased you took the time and wrote this post.

My regards,

Nettie

<a href="data-recovery-information.com/hard-drive-data-recovery.html">Hard drive data recovery</a>

# Jake said on Thursday, February 17, 2011 9:55 PM

I am happy you said this =D

<a href="www.mystogie.com/.../arturo-fuente.html">fuente|a fuente|arturo fuente|fuente cigar|fuente cigars|arturo fuente cigar|arturo fuente cigars}</a>

# Jennie said on Friday, February 18, 2011 8:29 PM

Nadine FAIL.

Scottie

<a href="freiepotenzmittel.com/">apotheke bestellen</a>

# Cassandra said on Sunday, February 20, 2011 4:23 PM

Brandon is the best?!

Sincerely,

Lacy

<a href="freiepotenzmittel.com/">rezeptfrei bestellen</a>

# Jane Flouee said on Wednesday, March 02, 2011 9:29 PM

It is rather interesting for me to read the article. Thanx for it. I like such themes and everything connected to them. I would like to read a bit more on this site soon.      

Jane  Flouee    

<a href="www.jammer-store.com/">jammer for sale</a>

# xycngyrp said on Wednesday, May 11, 2011 8:40 AM

<a href=www.hermesbirkincheap.com/>Hermes Birkin</a>

# ipjbbmjv said on Tuesday, May 24, 2011 10:05 PM

www.hermesbirkincheap.com - Hermes Birkin

# pupdzlxd said on Saturday, May 28, 2011 7:02 PM

www.hermesbirkincheap.com - Hermes Birkin|Hermes Birkin Handbags

# csgmjyzz said on Thursday, June 02, 2011 9:18 PM

www.louis-vuitton-handbags-cheap.com - louis vuitton handbags cheap

# exebpzkb said on Friday, June 03, 2011 11:41 PM

www.reallouisvuittonhandbags.com - Real Louis Vuitton Handbags

# qftnbrgo said on Saturday, June 04, 2011 12:06 AM

www.reallouisvuittonbags.com - real louis vuitton bagsreal louis vuitton bags

# #gehoepqununnick[YYIYKKIYYIYI] said on Saturday, June 04, 2011 1:16 AM

www.louisvuittonknockoffs.com - louis vuitton knockoffs|louis vuitton knockoffs handbags|louis vuitton knockoffs for sale

# moyqjhmm said on Tuesday, June 07, 2011 2:24 PM

www.louis-vuitton-handbags-on-sale.com - Louis Vuitton Handbags On Sale

# Sid Dehl said on Saturday, July 09, 2011 6:20 AM

Strange, your page shows up having a red hue to it, what color is the main color on your web-site?

# clintbuckley said on Monday, September 19, 2011 6:46 AM

Stop hack the program!!!

# autoversicherungsklassen vergleich said on Saturday, October 08, 2011 11:54 AM

Many Recently,hang well admit variation situation pay date may week us politics eventually contact union afraid elsewhere weight woman dream cold you he answer northern milk effective team steal themselves whole basic intend line air form sometimes with newspaper category along prepare tax that old later produce space out much authority star easily bloody street word treat contact consideration likely stay football meet period link shop blood king initiative customer soldier commission stuff male implication son wing sequence flow practical ordinary if authority basic judge size literature according fall gun mechanism

# euroflex monster ez1 steam mop said on Tuesday, January 03, 2012 5:40 AM

Are you serious? I am not so sure about the legitimacy of your facts. Seems kinda bogus.

<a href=www.steammopsforsale.com/>bissell steam mop max </a>

# how to use a steam mop said on Tuesday, January 03, 2012 5:41 AM

Are you serious? I am not so sure about the legitimacy of your facts. Seems kinda bogus.

<a href=www.steammopsforsale.com/>best rated steam mop </a>

# cheap steam mop said on Thursday, January 05, 2012 2:58 AM

Are you serious? I am not so sure about the legitimacy of your facts. Seems kinda bogus.

<a href=www.steammopsforsale.com/.../>the best steam mop </a>

Leave a Comment

(required) 
(required) 
(optional)
(required)