2010-08-10 51 views
3

希望你们大家可以为我澄清一些。我有一个使用Sql Membership Provider的Web应用程序,它通过WCF服务与第二个Web应用程序通信。这两个应用程序共享相同的Sql成员资格提供程序数据存储...但我需要每个WCF服务调用来认证用户。使用SQL成员资格提供程序的WCF身份验证

现在,我已经看了很多样本​​,但我觉得我看到的样本要么遗漏了某些代码,因为它“应该”对我很明显,或者我不明白WCF如何处理请求(请参阅下面的预期代码)。

我感谢任何帮助......

这里是我已经知道该怎么做

  1. 我知道如何在两端配置SQL成员资格。
  2. 我知道的wsHttpBinding
  3. 如何设置我知道如何安装在运输安全

这里使用的服务的证书是我很困惑ON

  • 如何通过会员密码(...您不能)
  • 我是否通过身份验证Cookie?如果是这样,怎么样?
  • 我是否创建与用户(他们自己)无关的“已知”用户名和密码?
  • Web客户端我希望看到CODE像这样

    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服务我希望看到CODE像这样

    [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; 
    } 
    
    +0

    你好,我在这里回答类似的问题:http://stackoverflow.com/questions/3460435/asp-net-to-wcf-passthrough-security – 2010-08-11 20:50:45

    回答

    1

    我假设你的WCF会员供应商和你的Web应用程序会员供应商正在使用相同的后端用户集。

    如果是这样,你会想在应用程序之间共享认证cookie。你可以找到更多的信息如何做到这一点here

    接下来,您需要将Web应用程序请求附带的身份验证cookie传递给WCF服务调用。你可以看到如何做到这一点here

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

    +0

    问:如何通过cookie的帮助执行上面的“SecurityAction.Demand”? – 2010-08-10 20:20:40

    +0

    其他示例在线“说”您可以从客户端传递凭据,WCF将检查Sql Membership并强制执行位于“SecurityAction.Demand”中的“Role”。我的问题是WCF如何知道如何做到这一点?它处理“引擎盖下”黑盒风格? – 2010-08-10 20:26:27

    +0

    当用户通过身份验证时,会创建一个实现IIdenty接口的对象。需求将使用此对象来执行检查。当用户使用cookie中的信息进行认证时,会员提供商将创建IIdentity对象。 – Daniel 2010-08-13 22:08:03

    0

    看看下面的链接中的“Web到远程WCF使用传输安全(受信任的子系统,HTTP)”,我相信你可能会在那里找到答案。还有下面的代码,我相信可以帮助:

    app.Context.User = new GenericPrincipal(new 
         GenericIdentity(username, "Membership Provider"), roles); 
    

    也:

    NetworkCredential netCred = new NetworkCredential("username", " [email protected]"); 
    asmxwebservice.Service proxy = new asmxwebservice.Service(); 
    proxy.Credentials = netCred;    
    proxy.GetData(21, true); 
    

    否则我会sugest回到基础,所以确保你获得预期的验证和授权配置工作100在asp客户端(asp.net应用程序)中使用成员资格和角色提供程序时的%。

    然后对WCF服务上的成员资格和角色使用相同的配置。唯一突出的部分是确保绑定正常工作。

    我还没有尝试过表单身份验证,但最近已经实现了从ASP.NET应用程序调用IIS中托管的WCF服务的实现Windows身份验证,并且我发现配置文件(绑定)中的一个小错误可能会导致您的应用程序打破。

    通过将用户上下文设置为通用主计算机,您应该能够在WCF服务端接收用户详细信息。

    我在一些文章的字里行间foud的答案,以便确保您看看下面的例子:

    希望这有助于