如何在ServiceStack认证之后我重定向重定向、如何在、ServiceStack

2023-09-03 11:14:42 作者:晴天阴天雨天

我已经覆盖了CredentialsAuthProvider像这样:

 公众覆盖布尔TryAuthenticate(IServiceBase authService,用户名字符串,字符串密码)
        {
            // TODO:验证用户,如果有效登录返回
            返回true;
        }

公众覆盖无效OnAuthenticated(IServiceBase authService,IAuthSession会议,IOAuthTokens令牌,字典<字符串,字符串> AUTHINFO)
        {
            base.OnAuthenticated(authService,会话令牌,AUTHINFO);

            //用户已被认证

            //查找用户的角色形成DB

            如果(roleA)
                // GOTO mypage1

            如果(roleB)
                // GOTO mypage2
        }
 

我执行一个简单的职位到〜/认证/资格证书,而认证工作和OnAuthenticated方法被调用时,我怎么居然将用户重定向到相应的页面基于一个角色或类似的东西?

我厌倦了做在OnAuthenticated方法如下,但它并没有产生预期的效果:

  

authService(/视图/客户);

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

 公共类CustomCredentialsAuthProvider:CredentialsAuthProvider
    {
        公众覆盖布尔TryAuthenticate(IServiceBase authService,用户名字符串,字符串密码)
        {
            返回true;
        }

        公众覆盖无效OnAuthenticated(IServiceBase authService,IAuthSession会议,IOAuthTokens令牌,字典<字符串,字符串> AUTHINFO)
        {
            session.ReferrerUrl =htt​​p://www.msn.com;

            base.OnAuthenticated(authService,会话令牌,AUTHINFO);
        }
    }
 

和形式POST:

 <形式方法=POSTACTION =/认证/资格证书和GT;
        <输入名称=用户名/>
        <输入名称=密码类型=密码/>
        <输入类型=提交/>
    < /形式GT;
 
微信公众号如何将 个人 迁移至 组织 ,你知道么

解决方案

在不同的地方,你可以设置的URL时重定向到ServiceStack验证,为了对precedence是:

的继续查询字符串,FORMDATA或提出请求时请求DTO变量 / AUTH Session.ReferrerUrl 网​​址 的HTTP 引用站点 HTTP头 在 CallbackUrl 中所使用的电流AuthProvider的项AuthConfig

鉴于preferences这些命令,如果该请求没有一个继续参数,它应该使用 session.ReferrerUrl ,所以你可以这样做:

 如果(roleA)session.ReferrerUrl =HTTP:// myPage1Url;
如果(roleB)session.ReferrerUrl =HTTP:// myPage2Url;
 

I've overridden the CredentialsAuthProvider like so:

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
        }

I perform a simple post to ~/auth/Credentials and while the authentication works and the OnAuthenticated method is called, how do I actually redirect the user to the appropriate page based on a role or something similar?

I tired to do the following in the OnAuthenticated method but it did not have the desired effect:

authService.("/views/customers");

Update using Starter Template (see comment below):

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);
        }
    }

And the form to POST:

<form method="POST" action="/auth/credentials">
        <input name="UserName"/>
        <input name="Password" type="password"/>
        <input type="submit"/>
    </form>

解决方案

The different places where you can set the Url to redirect to during ServiceStack Authentication, in order of precedence are:

The Continue QueryString, FormData or Request DTO variable when making the request to /auth The Session.ReferrerUrl Url The HTTP Referer HTTP Header The CallbackUrl in the AuthConfig of the current AuthProvider used

Given these order of preferences, if the request didn't have a Continue parameter, it should use the session.ReferrerUrl, so you could do:

if (roleA) session.ReferrerUrl = "http://myPage1Url";
if (roleB) session.ReferrerUrl = "http://myPage2Url";