[英]How do I redirect after authentication in ServiceStack
I've overridden the CredentialsAuthProvider like so: 我像这样覆盖了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
}
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? 我对〜/ auth / Credentials执行一个简单的帖子,当身份验证工作并调用OnAuthenticated方法时,如何根据角色或类似的东西将用户重定向到适当的页面?
I tired to do the following in the OnAuthenticated method but it did not have the desired effect: 我厌倦了在OnAuthenticated方法中执行以下操作,但它没有达到预期的效果:
authService.("/views/customers");
authService( “/视图/客户”)。
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: 和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: 您可以在ServiceStack身份验证期间按优先顺序将Url设置为重定向到的不同位置:
/auth
/auth
发出请求时, 继续 QueryString,FormData或Request DTO变量 Session.ReferrerUrl
Url Session.ReferrerUrl
Url Given these order of preferences, if the request didn't have a Continue parameter, it should use the session.ReferrerUrl
, so you could do: 根据这些首选项顺序,如果请求没有Continue参数,则应使用
session.ReferrerUrl
,因此您可以执行以下操作:
if (roleA) session.ReferrerUrl = "http://myPage1Url";
if (roleB) session.ReferrerUrl = "http://myPage2Url";
mythz, mythz,
Good call on making this OSS. 很好地致力于制作这个OSS。 :)
:)
You are correct regarding the order of precedence: 关于优先顺序你是对的:
So in my example, I didn't have the Continue QueryString, Form Data or Request DTO variable, and I didn't have the CallbackUrl, and certainly not the Session.ReferrerUrl because this is the first post of the Session. 所以在我的例子中,我没有Continue QueryString,Form Data或Request DTO变量,我没有CallbackUrl,当然也没有Session.ReferrerUrl,因为这是Session的第一篇文章。
From AuthService.cs
: 来自
AuthService.cs
:
var referrerUrl = request.Continue
?? session.ReferrerUrl
?? this.RequestContext.GetHeader("Referer")
?? oAuthConfig.CallbackUrl;
By default referrerUrl will have the Referer header value from the request. 默认情况下,referrerUrl将具有来自请求的Referer标头值。 And that is what is going to be assigned to the Location header further down the
Post
method of the AuthService.cs
: 这就是将在
AuthService.cs
的Post
方法中进一步分配给Location头的AuthService.cs
:
if (!(response is IHttpResult))
{
return new HttpResult(response) {
Location = referrerUrl
};
}
Once authenticated, and the session.ReferrerUrl
is set here the response will be sent to the client with the Location property above set to the original referrer, not the value below: 一旦经过身份验证,并在此处设置
session.ReferrerUrl
,响应将被发送到客户端,并将上面的Location属性设置为原始referrer,而不是以下值:
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
session.ReferrerUrl = "http://www.msn.com";
}
It's only on the second POST of the same session will the client navigate to www.msn.com (in this example) because the session has already been populated. 只有在同一会话的第二个POST上,客户端才会导航到www.msn.com(在此示例中),因为会话已经填充。 I think this:
我认为这:
var referrerUrl = request.Continue
?? session.ReferrerUrl
?? this.RequestContext.GetHeader("Referer")
?? oAuthConfig.CallbackUrl;
Needs to be determined after the call to auth. 需要在致电身份验证后确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.