使用asp.net身份验证servicestack(Use asp.net authenticati

2019-08-17 03:47发布

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

Answer 1:

我没有测试过这一点,但我认为它应该让你开始。 很高兴地站在我的任何措施纠正。

事情我认为你需要做的..

  • 为了验证对阵双方“系统”你需要设置窗体的cookie并保存ServiceStack会议。

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

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)。

CustomAuthRepository.cs(不完整的,但应该让你开始)

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配置方法。

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


文章来源: Use asp.net authentication with servicestack