使用asp.net身份验证servicestack [英] Use asp.net authentication with servicestack

查看:141
本文介绍了使用asp.net身份验证servicestack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了与窗体身份验证几毫秒的LightSwitch应用程序 - >创建aspnet_ *表中的SQL Server。
如何使用所定义的用户,密码,甚至成员,角色和应用程序的权利在servicestack - 应用

I have written a couple of ms lightswitch applications with forms authentication -> this creates aspnet_* tables in sql server. How can I use the defined users, passwords, maybe even memberships, roles and application rights in a servicestack - application?

推荐答案

我没有测试过这一点,但我认为它应该让你开始。关于我的任何步骤欣然站在纠正。

I have not tested this but I think it should get you started. Gladly stand corrected on any of my steps.

东西我认为你需要做的..

Things I think you will need to do..


  • 在为了验证对阵双方的系统',你将需要设置窗体的cookie并保存ServiceStack会话。

而不是调用的 FormsAuthentication.Authentiate()做类似下面。直到您完成所有的步骤,这将无法工作。

Instead of calling FormsAuthentication.Authentiate() do something like below. This won't work until you complete all the steps.

var apiAuthService = AppHostBase.Resolve<AuthService>();
apiAuthService.RequestContext = System.Web.HttpContext.Current.ToRequestContext();
var apiResponse = apiAuthService.Authenticate(new Auth
{
    UserName = model.UserName,
    Password = model.Password,
    RememberMe = false
});


  • 创建 IUserAuthRepository 的一个子类(从aspnet_ *表检索会员/用户/角色和填充ServiceStack为AuthUser)。

    • Create a subclass of IUserAuthRepository (for retrieving membership/user/roles from aspnet_* tables and filling ServiceStack AuthUser).
    • CustomAuthRepository.cs(不完整的,但应该让你开始)

      CustomAuthRepository.cs (incomplete, but should get you started)

      public class CustomAuthRepository : IUserAuthRepository
      {
          private readonly MembershipProvider _membershipProvider;
          private readonly RoleProvider _roleProvider;
      
          public CustomAuthRepository()
          {
              _membershipProvider = Membership.Provider;
              _roleProvider = Roles.Provider;
          }
      
          public UserAuth GetUserAuthByUserName(string userNameOrEmail)
          {
              var user = _membershipProvider.GetUser(userNameOrEmail, true);
              return new UserAuth {FirstName = user.UserName, Roles = _roleProvider.GetRolesForUser(userNameOrEmail).ToList() //FILL IN REST OF PROPERTIES};
          }
      
          public bool TryAuthenticate(string userName, string password, out UserAuth userAuth)
          {
              //userId = null;
              userAuth = GetUserAuthByUserName(userName);
              if (userAuth == null) return false;
      
              if (FormsAuthentication.Authenticate(userName, password))
              {
                  FormsAuthentication.SetAuthCookie(userName, false);
                  return true;
              }
      
              userAuth = null;
              return false;
          }
      //MORE METHODS TO IMPLEMENT...
      }
      


      • 电线认证为ServiceStack在APPHOST配置方法。

        • Wire Authentication up for ServiceStack in AppHost configure method.

          var userRep = new CustomAuthRepository();
          
          container.Register<IUserAuthRepository>(userRep);
          
          Plugins.Add(
              new AuthFeature(() => new AuthUserSession(),
                  new IAuthProvider[] {
                      new CredentialsAuthProvider() 
                  }
              ));
          


        • 这篇关于使用asp.net身份验证servicestack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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