如何在ServiceStack认证之后我重定向 [英] How do I redirect after authentication in ServiceStack

查看:250
本文介绍了如何在ServiceStack认证之后我重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经覆盖了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时重定向到<一个href="https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization">ServiceStack验证,为了对precedence是:

  1. 继续查询字符串,FORMDATA或提出请求时请求DTO变量 / AUTH
  2. Session.ReferrerUrl 网​​址
  3. 的HTTP 引用站点 HTTP头
  4. 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:

  1. The Continue QueryString, FormData or Request DTO variable when making the request to /auth
  2. The Session.ReferrerUrl Url
  3. The HTTP Referer HTTP Header
  4. 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";

这篇关于如何在ServiceStack认证之后我重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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