希望你们能为我澄清一些问题。我有一个使用Sql Membership Provider的Web应用程序,它通过WCF服务与第二个Web应用程序通信。两个应用程序共享相同的Sql Membership Provider数据存储库...但是我需要每个WCF服务调用来对用户进行身份验证。
现在,我看了很多示例,但是我觉得我看到的示例要么遗漏了某些代码,因为它对我来说应该“显而易见”,或者我误解了WCF如何处理请求(请参见预期)下面的代码)。
我很感谢您的帮助...
我已经知道该怎么做
这里是我对的困惑
我希望在Web客户中看到这样的代码
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;
}
最佳答案
我假设您的WCF成员资格提供程序和Web应用程序成员资格提供程序正在使用相同的后端用户集。
如果是这样,您将希望在应用程序之间共享身份验证cookie。您可以找到有关如何执行here的更多信息。
接下来,您需要将Web应用程序请求随附的身份验证cookie传递给WCF服务调用。您可以看到如何执行here。
这个想法是用户登录到Web应用程序,然后登录到WCF服务。
关于wcf - 使用SQL成员资格提供程序的WCF身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451578/