希望你们能帮我澄清一些。我有一个使用 Sql Membership Provider 的 Web 应用程序,它通过 WCF 服务与第二个 Web 应用程序对话。两个应用程序共享同一个 Sql Membership Provider 数据存储......但我需要每个 WCF 服务调用来验证用户身份。
现在,我查看了很多示例,但我觉得我看到的示例要么遗漏了某些代码,因为它“应该”对我来说很明显,或者我误解了 WCF 如何处理请求(见预期下面的代码)。
我很感激任何帮助...
这是我已经知道该怎么做
- 我知道如何在两端配置 Sql Membership。
- 我知道如何设置 wsHttpBinding
- 我知道如何设置用于传输安全的服务证书
这就是我的困惑
- 我怎样才能通过会员密码(......你不能)
- 我是否通过了身份验证 cookie?如果是这样,怎么做?
- 我是否创建与用户(他们自己)无关的“已知”用户名和密码?
在网络客户端中,我希望看到类似这样的代码
protected void btnLogin_Click(object sender, EventArgs e)
{
// Logging into the web-application is known and easy.
if (Membership.ValidateUser("MyUserName", "MyPassword"))
{
FormsAuthentication.SetAuthCookie("MyUserName", true);
FormsAuthentication.RedirectFromLoginPage("MyUserName", false);
}
}
protected ServiceReference1.Contractor getContractor(Int32 key)
{
// I expect something "like" this on the client call.
MembershipUser user = Membership.GetUser("MyUserName");
ServiceReference1.FishDataClient wcfService = new ServiceReference1.FishDataClient();
// You can't retreive the users password directly,
// nor can you get the hash from the SqlMembershipProvider.
wcfService.ChannelFactory.Credentials.UserName.UserName = user.UserName;
// So doing something like this would not be possible.
wcfService.ChannelFactory.Credentials.UserName.Password = "??????";
// So how is the web service going to authenticate the user from it's
// references to the same SqlMembershipProvider (database).
ServiceReference1.Contractor contractor = wcfService.GetContractor(key);
wcfService.Close();
wcfService = null;
return contractor;
}
在 WCF 服务中,我希望看到类似这样的代码
[PrincipalPermission(SecurityAction.Demand, Role = "User")]
public Contractor GetContractor(Int32 key)
{
ServiceSecurityContext context = ServiceSecurityContext.Current;
Contractor contractor = new Contractor();
// What goes here? I would expect something like this...
if (Membership.ValidateUser("???????", "???????"))
contractor.Get(key);
return contractor;
}