How can I pass logged on user details from ASP.Net web app to WCF service?

怎甘沉沦 提交于 2019-12-08 16:16:32

if I understood correctly, you need to pass your ASP.NET logged user credential to WCF service, right? Try this approach:


using (YourServiceClient client = new YourServiceClient())
{
     client.ClientCredentials.Windows.AllowedImpersonationLevel = 
          System.Security.Principal.TokenImpersonationLevel.Impersonation;
     client.ChannelFactory.Credentials.Windows.ClientCredential = 
          CredentialCache.DefaultNetworkCredentials;
     client.YourMethodHere();
}

You can definitely retrieve the currently logged on user in ASP.NET using the HttpContext.Current.User.Identity.Name property - trouble is, typically you don't have any way to retrieve the user's password to use for calling the WCF service.

It really depends on how you do your forms authentication. Do you have your own store? Does that store have user password's in it in a form that can be used, e.g. are the user's password stored in plain text (bad idea!) or in a reversible encryption schema?

Marc

I've been trying to do something similar in SharePoint. The best solution I've been able to come up with is:

 using (ChannelFactory<IMyContract> requestChannelFactory = new ChannelFactory<IMyContract>(binding, new EndpointAddress(endPointURL)))
 {
     provider = requestChannelFactory.CreateChannel();
     using (HostingEnvironment.Impersonate())
     {
         remoteData = provider.GetData();
     }
 }

This tries to authenticate me as DOMAIN\SERVER$ but unfortunately I haven't found a way to pull the client credentials out of HttpContext and shoehorn them into requestChannelFactory.Credentials.Windows.ClientCredential. I'm pretty sure it can't be done. The alternative seems to be hardcoding the username/password or using certificate authentication. That or allowing anonymous authentication which is not an option for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!