Search code examples
c#asp.net.netasmxclaims-based-identity

Read authentication info (Principal) by developing a .Net Web Service


I am dealing with a Claim-based application, with the aim to display users info after their authentication through SSO.

For a given authenticated user, I realized a .Net Web Page wherein I show all claims starting from the provided Principal (Page.User), as follows:

public partial class ClaimsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var claimsPrincipal = Page.User as IClaimsPrincipal;
        if (claimsPrincipal != null)
        {
            IClaimsIdentity claimsIdentity = (IClaimsIdentity) claimsPrincipal.Identity;
            // Something else...
        }
    }
}

And it works fine.

Now, I would like to do the same thing, but by using a .Net Web Service (SOAP), instead of the web page.

My current code is as follows:

[WebService(Namespace = "http://myapp/claims")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ClaimsWS : System.Web.Services.WebService
{
    [WebMethod]
    public List<ClaimValues> GetClaims()
    {
        // Get Principal..
        // Read claims...
    }
}

My question is: how can I obtain the current Principal inside the WebService body?


Solution

  • Maybe you can try something along these lines. I am returning an Identity object, but you get the claims list from it also.

    [WebService(Namespace = "http://myapp/claims")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class ClaimsWS : System.Web.Services.WebService
    {
        [WebMethod]
        public List<ClaimValues> GetClaims()
        {
            var principal =ClaimsPrincipal.CreateFromHttpContext(HttpContext.Current); 
            return principal.Claims;
        }
    }