473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

WCF problem maintaining principal across service calls

M#
Hi everyone,

I'm using WCF authentication services in my current project. I used
the following information as a starting point:
http://msdn.microsoft.com/en-us/library/bb398990.aspx

Unfortunately, I can't manage to get/set the generic principal when I
call other services. The authentication cookie is always null. I've
included some of my Global.asax code below. Any help would be greatly
appreciated:

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AuthenticationService.Authenticating
+= new EventHandler<AuthenticatingEventArgs>
(AuthenticationService_Authenticating);

/// Customize the cookie returned, adding role
/// and other information.
AuthenticationService.CreatingCookie
+= new EventHandler<CreatingCookieEventArgs>
(AuthenticationService_CreatingCookie);
}

void AuthenticationService_Authenticating(object sender,
AuthenticatingEventArgs e)
{
e.Authenticated = false;

/// Theoretical custom credential that would distinguish
/// duplicate user names for the purpose of ensuring
/// unique authentication MK 04/13/08
int organizationId;

string[] credentials = e.CustomCredential.Split(new char[]
{ ',' });

if (credentials.Length != 0
&& int.TryParse(credentials[0], out organizationId))
{
e.Authenticated =
MembershipManager.ValidateUser(e.UserName,
e.Password,
organizationId);
}

e.AuthenticationIsComplete = true;
}

void AuthenticationService_CreatingCookie(object sender,
CreatingCookieEventArgs e)
{
int organizationId;

if (int.TryParse(e.CustomCredential, out organizationId))
{
string roles = RoleManager.GetRolesForUser(e.UserName,
organizationId);

FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(1,
e.UserName,
DateTime.Now,
DateTime.Now.AddHours(2),
false,
roles,
FormsAuthentication.FormsCookiePath);

string encryptedTicket =
FormsAuthentication.Encrypt(ticket);

HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket);

cookie.Expires = DateTime.Now.AddHours(2);
cookie.Domain = ".localhost";
HttpContext.Current.Response.Cookies.Add(cookie);

e.CookieIsSet = true;
}
}

/// <summary
/// Recreates the Principal on every request, assigning roles
/// and other information from the authentication ticket.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie ticketCookie
=
Context.Request.Cookies[FormsAuthentication.FormsCookieName];

if (null == ticketCookie)
{
return;
}

try
{
FormsAuthenticationTicket ticket
= FormsAuthentication.Decrypt(ticketCookie.Value);

if (null != ticket)
{
string[] roles =
RoleManager.GetRolesFromString(ticket.UserData);
FormsIdentity identity = new FormsIdentity(ticket);
Context.User = new GenericPrincipal(identity, roles);
}
}
catch (Exception ex)
{
/// TODO: Make to call to exception utility method
}
}
Jun 1 '08 #1
0 1422

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Leo Tohill | last post by:
In our IIS application we have C# "behind code" that calls across remoting to a service. In the service, we keep local storage on the thread. From instrumentation, I now realize that hundreds of...
0
by: GnanaPrakash.R | last post by:
Hi All, I would like to trace all remoting calls happen from the client. I got a rough idea of creating custom service and injecting into the object/message chain. Now i want to pass the user...
0
by: Mythran | last post by:
We have a class that Implements IPrincipal (System.Security.Principal.IPrincipal). We have a business logic class library assembly that checks the Principal object for role information to see if...
0
by: Mythran | last post by:
I have a custom Principal (implements System.Security.Principal.IPrincipal interface) that I need to access from a WebService. My web application has the Thread.CurrentPrincipal set to this new...
0
by: google | last post by:
Hi, I use Custom Principal and it works well on my PC (Localhost). When I deploy it at my hosting service it fails. I print out HttpContext.Current.User.GetType().ToString() On my PC it...
1
by: giatorta | last post by:
Hi all, I have a scenario where the client calls Web Service WS1 and WS1 calls Web Service WS2 I have methods M1 on WS1 and M2 on WS2 both with EnabledSession=True assume M1 on WS1 does the...
2
by: ilyas | last post by:
Hi all I have a web service that I have written. It is written in C# I want to use this web service from a browser, but my users all have different browsers so its important that the code to...
3
by: M# | last post by:
Hi everyone, I'm using WCF authentication services in my current project. I used the following information as a starting point: http://msdn.microsoft.com/en-us/library/bb398990.aspx ...
3
by: eschneider | last post by:
Just some common issues with WS: Using custom objects: When objects change, seems you are always fixing some issue. Update references, which sometimes does not work. Deployment: Weird errors...
0
mbmccormick
by: mbmccormick | last post by:
I have a web service in the same project as the rest of my website. I am able to call my web service without any trouble from the Javascript on my page using the Script Manager. Now, I want to have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.