how to redirect after authentication

777 views
Skip to first unread message

grendo

unread,
May 19, 2012, 6:52:57 AM5/19/12
to ServiceStack .NET Open Source REST Web Services Framework
Hi,
I am trying to use the service stack authentication module to
authenticate mvc pages and some rest services inside the mvc project.
I have it largely going Except for one part. When post the form to
login to "/api/auth/credentials" it logs in correctly ends up going to
the /api/auth/credentials page. How do I get it to redirect to the
calling page ? can I post the redirect url in the form ?

Thanks

grendo

unread,
May 20, 2012, 3:22:17 AM5/20/12
to ServiceStack .NET Open Source REST Web Services Framework
Just adding a bit more information. I traced the code through and in
the OnPost in AuthService class the code is returning in the call to
oAuthConfig.Authorize and ending up at the /api/auth/credentials
page, (see code below). I may not be using the auth service correctly
but after the authentication has taken place I wanted it to return
back to what I put in the Continue or referrer url.

var session = this.GetSession();
if (!oAuthConfig.IsAuthorized(session,
session.GetOAuthTokens(provider), request))
{
return oAuthConfig.Authenticate(this, session, request);
}

var referrerUrl = request.Continue
?? session.ReferrerUrl
?? this.RequestContext.GetHeader("Referer")
?? oAuthConfig.CallbackUrl;

Demis Bellot

unread,
May 20, 2012, 4:04:32 AM5/20/12
to servic...@googlegroups.com
Hi,

I've just added the Continue field so you could specify the redirect on the Url - but you have to get the latest from NuGet v3.78 to try it. 
Note: You could always set a permanent url in the oAuthConfig.CallbackUrl configuration.

So can you make sure you're using the latest code, and try using the Continue field?

Cheers,

grendo

unread,
May 20, 2012, 4:17:13 AM5/20/12
to ServiceStack .NET Open Source REST Web Services Framework
Hi Dennis,
I made sure I have 3.78 and added

<add key="oauth.credentials.CallbackUrl" value="/api/home" />

to the web config, (note I am only using the credentials provider).
Is this how you configure the CallbackUrl ?

grendo

unread,
May 20, 2012, 7:52:34 AM5/20/12
to ServiceStack .NET Open Source REST Web Services Framework
Hi Dennis,
I had the config setting wrong in the last post, it should be /home
not /api/home

<add key="oauth.credentials.CallbackUrl" value="/home" />

I could not get the redirect to work without adjusting the code in
AuthService.cs OnPost method by moving the referrerUrl variable to
before !isAuthorized check and added a redirect after the call to the
oAuthConfig.Authenticate, (needs some error checking to make sure the
authenticate method was successful).

Does this look like a bug or am I using the service wrong ?

Code is below

var referrerUrl = request.Continue
?? session.ReferrerUrl
?? this.RequestContext.GetHeader("Referer")
?? oAuthConfig.CallbackUrl;
if (!oAuthConfig.IsAuthorized(session,
session.GetOAuthTokens(provider), request))
{
var ret = oAuthConfig.Authenticate(this, session, request) as
AuthResponse;
return this.Redirect(referrerUrl.AddHashParam("s", "0"));
}

//Already Authenticated
if (base.RequestContext.ResponseContentType == ContentType.Html)
return this.Redirect(referrerUrl.AddHashParam("s", "0"));



Demis Bellot

unread,
May 21, 2012, 2:15:52 AM5/21/12
to servic...@googlegroups.com
I've just made an update to get Redirects to work better. 
Try v3.79 on NuGet.

Cheers,

grendo

unread,
May 21, 2012, 7:19:23 AM5/21/12
to ServiceStack .NET Open Source REST Web Services Framework
I gave 3.79 and works great.

Thanks for your help Demis.
Reply all
Reply to author
Forward
0 new messages