WCF problem maintaining principal across service calls

M

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
}
}
 
M

M#

Ok, so I'm guessing that I was a little unclear about the above. To
put it more succinctly: I'm trying to use a forms authentication
cookie with my website AND the multiple web services to authenticate
and authorize users. Is this possible, and if so how?

Thanks,
M#
 
M

M#

strictly speaking? No. But there is a workaround here that you ought to try
outhttp://www.dotnetbips.com/articles/dbd724e9-78f0-4a05-adfb-190d151103...

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively onwww.lulu.com/owc$19.99
-------------------------------------------------------


Ok, so I'm guessing that I was a little unclear about the above. To
put it more succinctly: I'm trying to use a forms authentication
cookie with my website AND the multiple web services to authenticate
and authorize users. Is this possible, and if so how?
Thanks,
M#

Thanks for the response. Unfortunately I can't use the .asmx style
services. My requirements are that I use WCF service to authenticate
and authorize the user. The WCF is being hosted in separate
application and server, than the consuming application. When the user
is authenticated I would like to maintain the principal across threads
and subsequent calls to other wcf services. I know that's kinda a
plateful, but do know of any other places I can look to resolve this?

Thanks again,
M#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top