Discussion how to get current username or userid using WCF channnel Factory?

  • Wednesday, September 12, 2012 3:24 AM
     
     

    Hi,

    I am not able to get username or user id using HttpContext.Current.Request.ServerVariables["HTTP_USER"] using WCF channel factory as HttpContext.Current is null.but if i added service referrence object in my application and calling wcf operations  then i am able to get username using  HttpContext.Current.Request.ServerVariables["HTTP_USER"] on server side. If i use channel factory i am getting HttpContext.Current is null. So Can any one please tell me how to get current username using WCF Channel Factory on server side?

     


    ammu


    • Edited by ammu sree Wednesday, September 12, 2012 3:25 AM
    •  

All Replies

  • Wednesday, September 12, 2012 3:38 AM
     
      Has Code

    HttpContext is an ASP.NET concept and doesn't apply to WCF unless you run it with ASP.NET Compatibility turned on.

    You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration:

    <configuration>   
    <system.serviceModel>     
    		<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>   
    	</system.serviceModel> 
    </configuration>


    You can even enforce AspNetCompatibility by decorating your service class with an additional attribute:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

    Another way is to use WCF ServiceSecurityContext.Current.PrimaryIdentity or get the security context via the OperationContext. But it's useful if you are using seperate WCF authentication mechanism.

    Below are details for OperationContext and ServiceContext

    http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.aspx
    http://msdn.microsoft.com/en-us/library/system.servicemodel.servicesecuritycontext.aspx

    Hope, this will help you out. Feel free to contact in case of query.


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

  • Wednesday, September 12, 2012 6:25 AM
     
     
    Tejas T
    Avatar of Tejas T

    Tejas T

    IGate

    365 Points 8 0 0
    Recent Achievements
    Forums Answerer II Profile Complete Forums Replies I

    Thanks for your reply. I am using wcf service in silverlight application and declared all properties whcih are required for service to run even i specified earlier above preoperties in my asp.net web.config. So if i want to get username in server using wcf channnel factory other than by creating service referrence at client side and calling wcf methods using channel factory and in that wcf method i need current username who is logged in at server side using OperationContext or ServiceSecurityContext ? can you please send me any code snippet ASAP?


    ammu

  • Wednesday, September 12, 2012 1:25 PM
     
     

    You can get it through

    OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name

    For more reference and code below reference may be useful to you.

    http://weblogs.asp.net/dwahlin/archive/2011/04/14/working-with-user-names-and-roles-in-silverlight-applications.aspx

    http://msdn.microsoft.com/en-us/library/dd744835(v=vs.95).aspx

    Hope this will help you out. Feel free to contact in case of query.


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

  • Friday, September 14, 2012 9:28 AM
     
     

    Tejas T,

    Even i am not getting current username using OperationContext.Current as it is null as same as HttpConetxt.current object. Can you please suggest other way of getting it with sample code?


    ammu

  • Thursday, September 20, 2012 4:22 AM
     
     

    Tejas T,

    Any updates on this as i am waiting for your reply as OperationContext.Current  or HttpConetxt.current objects are null in my case when i am using channel factory.

    Ammu


    ammu