wcf ria forms验证的处理方式

本文讨论了在ASP.NET应用中使用自定义验证类时,客户端获取当前用户代码出现的问题及解决方案。通过比较默认验证方式与自定义验证方式的区别,提出了解决兼容性问题的方法,确保了应用的稳定性和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用模板

[EnableClientAccess]
public class AuthenticationDomainService : AuthenticationBase<User>
{

在客户端app.cs里加上

public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;

InitializeComponent();
var webContext = new WebContext();
var fa = new FormsAuthentication();
webContext.Authentication = fa;
this.ApplicationLifetimeObjects.Add(webContext);
}

2.自定义一个验证类,添加一个CS文件

[EnableClientAccess]
public class AuthenticationDomainService1 : DomainService, IAuthentication<AuthUser>
{

public AuthUser GetUser()
{
throw new NotImplementedException();
}

public AuthUser Login(string userName, string password, bool isPersistent, string customData)
{
throw new NotImplementedException();
}

public AuthUser Logout()
{
throw new NotImplementedException();
}

public void UpdateUser(AuthUser user)
{
throw new NotImplementedException();
}
}

这里有个问题就是 自定义的User类不能在二个domainservice里使用,如上面<authuser>改成user后,在客户端会生成二个相同的属性,就会出错。因此,自己又加了一个继承userbase的类

在app.cs里要指定,用哪个类来处理验证

public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;

InitializeComponent();
var webContext = new WebContext();
var fa = new FormsAuthentication();
webContext.Authentication = fa;
fa.DomainContext = new AuthenticationDomainService1();//指定验证类
this.ApplicationLifetimeObjects.Add(webContext);
}

通过以上二种方式来实现验证。但有一个问题,如自定义验证,那客户端获取当前用户的代码在形式上与默认方式上有些不同

WebContext.Current.User;

WebContext.Current.Authentication.User;

代码上有时还有点兼容性问题,不得不去改一下。这一点感觉不是很好。(应该是服务器有二个实现IAuthentication接口的domainservice时引起的。只有一个的时候,可以看到webcontext.current.user,如有二个时,则看不到。

怎么感觉上去,很不稳定似的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值