使用 SQL 成员身份提供程序的 WCF 身份验证 [英] WCF Authentication using SQL Membership Provider

查看:27
本文介绍了使用 SQL 成员身份提供程序的 WCF 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你们能帮我澄清一些.我有一个使用 Sql Membership Provider 的 Web 应用程序,它通过 WCF 服务与第二个 Web 应用程序通信.两个应用程序共享相同的 Sql Membership Provider 数据存储区……但我需要每个 WCF 服务调用来验证用户身份.

Hopefully you folks can clarify some of this for me. I have a web-application using the Sql Membership Provider and it talks to a second web-application through a WCF Service. Both applications share the same Sql Membership Provider datastore...but I need each WCF Service call to authenticate the user.

现在,我已经查看了很多示例,但我觉得我所看到的示例要么遗漏了某些代码,因为它应该"对我来说很明显,要么我误解了 WCF 如何处理请求(请参阅下面的预期代码).

Now, I've looked at a LOT of samples, but I feel like the samples I've seen either leave out certain code because it "should" be obvious to me, or I mis-understand how WCF handles the request (see expected code below).

感谢您的帮助...

这是我已经知道的操作

  1. 我知道如何在两端配置 Sql Membership.
  2. 我知道如何设置 wsHttpBinding
  3. 我知道如何设置用于传输安全的服务证书

这就是我的困惑

  1. 我如何传递会员密码(...你不能)
  2. 我是否通过了身份验证 cookie?如果是这样,怎么办?
  3. 我是否创建了一个与用户(他们自己)无关的已知"用户名和密码?

我希望在网络客户端中看到类似这样的代码

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 应用程序成员资格提供程序使用相同的后端用户集.

I'm assuming your WCF membership provider and your web application membership provider are using the same backend user set.

如果是这样,您希望在应用程序之间共享身份验证 cookie.您可以在此处找到有关如何执行此操作的更多信息.

If so you would want to share authentication cookies between the applications. You can find more information on how to do that here.

接下来,您需要将 Web 应用程序请求附带的身份验证 cookie 传递给 WCF 服务调用.您可以在此处了解如何做到这一点.

Next you need to pass the authentication cookie that came with the web application request to the WCF service call. You can see how to do that here.

这个想法是用户登录到您的 Web 应用程序,并通过这样做登录到您的 WCF 服务.

The idea is the user logs into your web application and by doing so is logged into your WCF service.

这篇关于使用 SQL 成员身份提供程序的 WCF 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆