wcf ria forms验证的处理方式

来源:互联网 发布:vm桥接模式 无网络 编辑:程序博客网 时间:2024/05/06 22:55
 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,如有二个时,则看不到。

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

原创粉丝点击