WCF problem maintaining principal across service calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M#

    WCF problem maintaining principal across service calls

    Hi everyone,

    I'm using WCF authentication services in my current project. I used
    the following information as a starting point:


    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_Sta rt(object sender, EventArgs e)
    {
    // Code that runs on application startup
    AuthenticationS ervice.Authenti cating
    += new EventHandler<Au thenticatingEve ntArgs>
    (Authentication Service_Authent icating);

    /// Customize the cookie returned, adding role
    /// and other information.
    AuthenticationS ervice.Creating Cookie
    += new EventHandler<Cr eatingCookieEve ntArgs>
    (Authentication Service_Creatin gCookie);
    }

    void AuthenticationS ervice_Authenti cating(object sender,
    AuthenticatingE ventArgs 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.CustomCredent ial.Split(new char[]
    { ',' });

    if (credentials.Le ngth != 0
    && int.TryParse(cr edentials[0], out organizationId) )
    {
    e.Authenticated =
    MembershipManag er.ValidateUser (e.UserName,
    e.Password,
    organizationId) ;
    }

    e.Authenticatio nIsComplete = true;
    }

    void AuthenticationS ervice_Creating Cookie(object sender,
    CreatingCookieE ventArgs e)
    {
    int organizationId;

    if (int.TryParse(e .CustomCredenti al, out organizationId) )
    {
    string roles = RoleManager.Get RolesForUser(e. UserName,
    organizationId) ;

    FormsAuthentica tionTicket ticket = new
    FormsAuthentica tionTicket(1,
    e.UserName,
    DateTime.Now,
    DateTime.Now.Ad dHours(2),
    false,
    roles,
    FormsAuthentica tion.FormsCooki ePath);

    string encryptedTicket =
    FormsAuthentica tion.Encrypt(ti cket);

    HttpCookie cookie = new HttpCookie(
    FormsAuthentica tion.FormsCooki eName,
    encryptedTicket );

    cookie.Expires = DateTime.Now.Ad dHours(2);
    cookie.Domain = ".localhost ";
    HttpContext.Cur rent.Response.C ookies.Add(cook ie);

    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_Aut henticateReques t(object sender, EventArgs e)
    {
    HttpCookie ticketCookie
    =
    Context.Request .Cookies[FormsAuthentica tion.FormsCooki eName];

    if (null == ticketCookie)
    {
    return;
    }

    try
    {
    FormsAuthentica tionTicket ticket
    = FormsAuthentica tion.Decrypt(ti cketCookie.Valu e);

    if (null != ticket)
    {
    string[] roles =
    RoleManager.Get RolesFromString (ticket.UserDat a);
    FormsIdentity identity = new FormsIdentity(t icket);
    Context.User = new GenericPrincipa l(identity, roles);
    }
    }
    catch (Exception ex)
    {
    /// TODO: Make to call to exception utility method
    }
    }
  • M#

    #2
    Re: WCF problem maintaining principal across service calls

    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#

    Comment

    • Alvin Bruney [ASP.NET MVP]

      #3
      Re: WCF problem maintaining principal across service calls

      strictly speaking? No. But there is a workaround here that you ought to try
      out
      Forms authentication works very well with web forms. Can you use it with web services? With some tricks - Yes. In fact in this article I am going to show how to do just that.


      --

      Regards,
      Alvin Bruney [MVP ASP.NET]

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


      "M#" <michauxkelley@ gmail.comwrote in message
      news:cec511be-259e-4679-9da0-f72c8ba464e6@59 g2000hsb.google groups.com...
      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#

      Comment

      • M#

        #4
        Re: WCF problem maintaining principal across service calls

        On Jun 7, 10:30 pm, "Alvin Bruney [ASP.NET MVP]" <vapor dan using hot
        male spam filterwrote:
        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
        -------------------------------------------------------
        >
        "M#" <michauxkel...@ gmail.comwrote in message
        >
        news:cec511be-259e-4679-9da0-f72c8ba464e6@59 g2000hsb.google groups.com...
        >
        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#

        Comment

        Working...