473,508 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1429

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

Similar topics

0
1269
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
1401
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
910
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
1413
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
1072
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
1241
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
1608
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
192
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
1636
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
823
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
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
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
7391
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...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.