【发布时间】:2011-01-28 02:35:57
【问题描述】:
我正在创建一个具有 WCF 服务后端的大型 ASP.NET 4 站点。我希望该站点使用该服务进行登录和授权。
基本上当用户进入登录页面时,我希望 WCF 服务用作身份验证机制,而不是使用成员资格提供程序。
WCF 服务已使用身份验证和授权(使用模拟),因此如果服务的客户端尝试执行他们无权执行的服务方法,则会收到错误消息。
问题是如何将它与 ASP.NET 网站联系起来?
这是我正在使用的一些示例代码:
网络服务接口:
[ServiceContract]
public interface IService
{
[OperationContract]
bool Foo();
[OperationContract]
void Bar();
}
网络服务:
public class BackEndService : IService
{
[PrincipalPermission(SecurityAction.Demand, Role="FooRole")]
public bool Foo()
{
return false;
}
[PrincipalPermission(SecurityAction.Demand, Role="Bar")]
public void Bar()
{
//...
}
}
在客户端:
public class LoginPage : Page
{
public void LoginButton_Clicked(object sender, EventArgs e)
{
string username = TxtUsername.Value;
string password = TxtPassword.Value;
BackEndService client = new BackEndService();
client.CleintCredentials.Username = username;
client.ClientCredentials.Password = password;
client.Foo(); //if the credentials supplied do not have permissions there will be error
client.Bar();
}
}
我的目标是用 PrincipalPermission 属性标记一些 UI 元素的功能,这样用户就不必前往服务来发现他们没有权限。
此外,我希望根据用户的权限加载/卸载一些 UI 元素。
是否有可能,或者我是否必须在我的服务中加入一些逻辑才能返回用户可以看到的任何 UI 模块?是否可以通过服务处理权限,就好像它是会员资格提供者一样,因为在 UI 元素的代码后面使用 PrincipalPermissionAttribute 就是这种情况?
谢谢,
<bleepzter/>
【问题讨论】:
标签: c# wcf asp.net-membership