PDA

View Full Version : Xin hỏi về WCF



nvkhanh.hcmc
10-11-2011, 21:47
Mình đang làm project về web sử dụng dịch vụ của WCF .Mình gặp rắc rối khi khai báo client sử dụng dịch vụ.
Cụ thể như sau :

Service client = new Service();
client. ClientCredentials . UserName . UserName = "abc";
client . ClientCredentials . UserName . Password = "abc";
client . foo1();

client . ClientCredentials . UserName . UserName = "abc";
client . ClientCredentials . UserName . Password = "abc";
client . foo2();

Vấn đề của mình là mỗi lần gọi 1 chức năng nào đó của service đều phải khai báo username và passord ( theo yêu cầu bảo mật của service ) .Mọi người biết cách nào khai báo username và password 1 lần mà vẫn gọi dc các chức năng của dịch vụ dc ko ??

Scripted
11-11-2011, 03:48
cái này thuộc về coder ... nếu login rồi có thể thâu vào session rồi website check session xem từ URL nào đến ... nếu chưa verify thì verify ... còn verify rồi thì bypass nó ...

littleNeo
03-01-2012, 01:56
WCF ko cho phép bạn set Username và Password của UserName credential trong file config, bạn chỉ có thể set trong code. Cách đơn giản nhất là tạo 1 Factory Method để initialize Service client cho bạn.



static class ServiceFactory
{
public static Service CreateClient()
{
Service client = new Service();
client. ClientCredentials . UserName . UserName = "abc";
client . ClientCredentials . UserName . Password = "abc";
return client;
}
}

....
using(Service client = ServiceFactory.CreateClient())
{
client.foo1();
}


Lâu rồi ko viết C#, có sai gì lượng thứ nhé


@Scripted: Lâu lâu ghé ddth, thấy bác chả khá hơn miếng nào ! buồn thật :no: .