需要ServiceStack的RegistrationFeature的管理员角色

时间:2019-04-02 10:24:09

标签: servicestack

如何在不允许自我注册的情况下将用户添加到<!-- Yandex.Metrika counter --> <script type="text/javascript" > (function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)}; m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)}) (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym"); ym(41441674, "init", { clickmap:true, trackLinks:true, accurateTrackBounce:true, webvisor:true }); </script> <noscript><div><img src="https://mc.yandex.ru/watch/41441674" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <!-- /Yandex.Metrika counter --> <script type="text/javascript"> document.addEventListener( 'wpcf7mailsent', function( event ) { ym(41441674, 'reachGoal', 'weddings'); return true; console.log('weddings'); }); var wpcf7Elm = document.querySelector( '.wpcf7' ); wpcf7Elm.addEventListener ( 'wpcf7mailsent', function( event ) { ym(41441674, 'reachGoal', 'zayavka'); console.log('zayavka'); }, false); document.addEventListener ( 'wpcf7submit', function( event ) { ym(41441674, 'reachGoal', 'zayavka'); console.log('wpcf7submit'); }, false); </script> ?是否有一种方法要求IAuthRepository具有特定角色(例如admin),还是仅在本地主机上运行仅RegistrationFeature的单独项目会更容易?

2 个答案:

答案 0 :(得分:2)

如果您不希望用户能够注册自己,那么您可能不想启用默认情况下未注册的RegistrationFeature本身。

using the IAuthRepository itself in your own Service to create users,您将拥有更大的灵活性。

ServiceStack's new Auth Templates显示了在启动时使用IAuthRepository创建用户的示例,例如:

if (authRepo.GetUserAuthByUserName("user@gmail.com") == null)
{
    var testUser = authRepo.CreateUserAuth(new UserAuth
    {
        DisplayName = "Test User",
        Email = "user@gmail.com",
        FirstName = "Test",
        LastName = "User",
    }, "p@55wOrd");
}

if (authRepo.GetUserAuthByUserName("manager@gmail.com") == null)
{
    var roleUser = authRepo.CreateUserAuth(new UserAuth
    {
        DisplayName = "Test Manager",
        Email = "manager@gmail.com",
        FirstName = "Test",
        LastName = "Manager",
    }, "p@55wOrd");
    authRepo.AssignRoles(roleUser, roles:new[]{ "Manager" });
}

if (authRepo.GetUserAuthByUserName("admin@gmail.com") == null)
{
    var roleUser = authRepo.CreateUserAuth(new UserAuth
    {
        DisplayName = "Admin User",
        Email = "admin@gmail.com",
        FirstName = "Admin",
        LastName = "User",
    }, "p@55wOrd");
    authRepo.AssignRoles(roleUser, roles:new[]{ "Admin" });
}

答案 1 :(得分:0)

我想我已经知道了:

typeof(RegisterService).AddAttributes(new RequiredRoleAttribute("Admin"));

更多内容:https://docs.servicestack.net/restricting-services