Możliwość przenoszenia głównego obiektu od klienta do strony usługi w WCF
W WCF po stronie klienta użytkownik zostanie uwierzytelniony, a jego role / uprawnienia będą przechowywane w obiektach Principal / Identity po stronie klienta. Po uwierzytelnieniu użytkownik powinien być w stanie wywołać metodę serwisową tylko wtedy, gdy pełni określoną rolę. Aby tak się stało, muszę przesłać obiekty klienta / tożsamości po stronie klienta do strony usługi. Ale gdy dotrę do strony serwisowej, głównym obiektem jest Windows Principal, a Identity to Windows Identity. Nie pozwala mi to sprawdzić, czy metoda usługi powinna być wywoływana na podstawie poświadczeń po stronie klienta.
Czy możliwe jest przeniesienie mojego obiektu głównego i obiektu tożsamości ze strony klienta na stronę serwera? Chcę przesłać mój główny obiekt (Generic Principal) na stronę serwera. Czy to możliwe? Proszę pomóż.
Wcześniej zamieściłem podobne pytanie w następujący sposób:
Przenoszenie dostosowanego obiektu głównego po stronie klienta do strony usługi WCF
Starałem się śledzić odpowiedzi, ale nie byłem w stanie przenieść mojego głównego obiektu.
Oto szczegóły.
Nastrona klienta mój główny obiekt i obiekt tożsamości wygląda następująco w oknie Natychmiastowym podczas debugowania:
System.Threading.Thread.CurrentPrincipal {System.Security.Principal.GenericPrincipal} [System.Security.Principal.GenericPrincipal]: {System.Security.Principal.GenericPrincipal} Identity: {System.Security.Principal.GenericIdentity} System.Threading. Thread.CurrentPrincipal.Identity {System.Security.Principal.GenericIdentity} [System.Security.Principal.GenericIdentity]: {System.Security.Principal.GenericIdentity} AuthenticationType: "" IsAuthenticated: false Name: ""
Naserwer, mój główny obiekt a tożsamość wygląda następująco:
System.Threading.Thread.CurrentPrincipal {System.Security.Principal.WindowsPrincipal} [System.Security.Principal.WindowsPrincipal]: {System.Security.Principal.WindowsPrincipal} Identity: {System.Security.Principal.WindowsIdentity} {System.Security .Principal.WindowsIdentity} [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity} AuthenticationType: "NTLM" IsAuthenticated: true Nazwa: „MyDomain MyLoginID”
Mój klient WCF wygląda następująco
Kod klienta:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client client = new Service1Client("NetTcpBinding_IService1");
Console.WriteLine(client.GetData(6548));
Console.ReadLine();
}
}
}
Konfiguracja klienta wygląda następująco:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1" closeTimeout="10:10:00"
openTimeout="10:10:00" receiveTimeout="10:10:00" sendTimeout="10:10:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="10:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8888/Service1" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService1" contract="ServiceReference1.IService1"
name="NetTcpBinding_IService1">
</endpoint>
</client>
</system.serviceModel>
</configuration>
Kod usługi wygląda następująco:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}