如何在ServiceStack中进行身份验证后重定向

leg*_*ion 9 .net c# servicestack

我像这样覆盖了CredentialsAuthProvider:

public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            //TODO: Auth the user and return if valid login
            return true;
        }

public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
        {
            base.OnAuthenticated(authService, session, tokens, authInfo);

            //User has been authenticated

            //Find the user's role form the DB

            if (roleA)
                //GOTO mypage1

            if (roleB)
                //GOTO mypage2
        }
Run Code Online (Sandbox Code Playgroud)

我对〜/ auth/Credentials执行一个简单的帖子,当身份验证工作并调用OnAuthenticated方法时,如何根据角色或类似的东西将用户重定向到适当的页面?

我厌倦了在OnAuthenticated方法中执行以下操作,但它没有达到预期的效果:

authService( "/视图/客户").

使用入门模板更新(请参阅下面的评论):

public class CustomCredentialsAuthProvider : CredentialsAuthProvider
    {
        public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            return true;
        }

        public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
        {
            session.ReferrerUrl = "http://www.msn.com";

            base.OnAuthenticated(authService, session, tokens, authInfo);
        }
    }
Run Code Online (Sandbox Code Playgroud)

和POST的形式:

<form method="POST" action="/auth/credentials">
        <input name="UserName"/>
        <input name="Password" type="password"/>
        <input type="submit"/>
    </form>
Run Code Online (Sandbox Code Playgroud)

myt*_*thz 7

您可以在ServiceStack身份验证期间按优先顺序将Url设置为重定向到的不同位置:

  1. 继续发出请求时查询字符串,FORMDATA或请求DTO变量/auth
  2. Session.ReferrerUrl网址
  3. HTTP Referer HTTP标头
  4. 使用当前AuthProvider的AuthConfig中的CallbackUrl

鉴于这些首选项顺序,如果请求没有Continue参数,它应该使用session.ReferrerUrl,所以你可以这样做:

if (roleA) session.ReferrerUrl = "http://myPage1Url";
if (roleB) session.ReferrerUrl = "http://myPage2Url";
Run Code Online (Sandbox Code Playgroud)