Bruce Hunter - 59 - Sep 12, 2014

I’m using WebForms and ServiceStack Interface as my backend on /api/authenticate.

I created a login.aspx page and on the code behind I’m using the Client to call authenticate. 

I have a base page class hooked into the Session. 

if (!this.UserSession.IsAuthenticated) this.RedirectToLogin();

I know that the Session is getting set to IsAuthenticated in the CustomCredentials class I created.

Not sure why IsAuthenticated isn’t sticking.

Bruce Hunter:

When using WebForms am I supposed to handle the cookie?

Bruce Hunter:

My Session Hookup in PageBase Class.


        /// <summary>
        /// Typed UserSession
        /// </summary>
        private object userSession;
        protected virtual TUserSession SessionAs<TUserSession>()
        {
            return (TUserSession)(userSession ?? (userSession = Cache.SessionAs<TUserSession>()));
        }

        protected CustomUserSession UserSession
        {
            get
            {
                return SessionAs<CustomUserSession>();
            }
        }

        protected new ICacheClient Cache
        {
            get { return HostContext.Resolve<ICacheClient>(); }
        }

        private ISessionFactory sessionFactory;
        protected virtual ISessionFactory SessionFactory
        {
            get { return sessionFactory ?? (sessionFactory = HostContext.Resolve<ISessionFactory>()) ?? new SessionFactory(Cache); }
        }

        /// <summary>
        /// Dynamic Session Bag
        /// </summary>
        private ISession session;
        protected new ISession Session
        {
            get
            {
                return session ?? (session = SessionFactory.GetOrCreateSession());
            }
        }

Bruce Hunter:

Let me go check my Save Session with OnAuthenticated

Bruce Hunter:

Yeah, I’m saving the Session in OnAuthenticated

Hi Bruce I’ve just added a new ASP.NET Base Page with access to most of ServiceStack providers inside ServiceStack.dll called ServiceStackPage https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/AspNet/ServiceStackPage.cs

I’ve also added a new WebForms test project mimicking your setup with SS hosted at /api the Default.aspx page shows how to authenticate with SS Auth Providers at: https://github.com/ServiceStack/Test/blob/master/src/WebForms/WebForms/Default.aspx
The page also includes an Ajax call to call ServiceStack services so you can verify the User is Authenticated in both WebForms and SS.
The code-behind shows the code to authenticate with ServiceStack via WebForms: https://github.com/ServiceStack/Test/blob/master/src/WebForms/WebForms/Default.aspx.cs

There’s also an example of an AuthOnly.aspx WebForm page that now respects the [Authenticate] attribute where it will only allow Authenticated users otherwise it will redirect you to the Default.aspx page to login.

You can play with this app with the online demo published at: http://webforms.servicestack.net/

You’ll need the latest v4.0.32+ packages on MyGet to get the new ServiceStackPage: https://github.com/ServiceStack/ServiceStack/wiki/MyGet

Bruce Hunter:

Dude you rock. I’m off to read code now! Thanks! I’ll let you know the outcome.

Bruce Hunter:

Looks like this Base page allows me to utilize the htmlRedirect option

Bruce Hunter:

This is needed?

<add key=“webPages:Enabled” value=“false” />

This is only if using Razor and not ASP.net webforms?

Not for non-razor web apps, but I always include it to stop it hijacking requests for razor pages.

Bruce Hunter:

                    var response = authService.Authenticate(new Authenticate
                    {
                        provider = CredentialsAuthProvider.Name,
                        UserName = txtUserName.Text,
                        Password = txtPassword.Text,
                        Continue = "WWW.GOOGLE.COM,
                        RememberMe = false,                        
                    });

                    // add ASP.NET auth cookie
                    FormsAuthentication.SetAuthCookie(txtUserName.Text, true);  

I’m assuming that the use of “Continue” with WebForms is out? Not working. Plus, if I set it then the cookie logic might not work? My goal is to Authenticate then redirect to a dashboard.aspx. I guess I can manually redirect after the SetAuthCookie method?

Yep, just redirect manually after SetAuthCookie method:
base.Response.Redirect(“http://google.com”);

Calling the higher-level API authService.Post(new Authenticate { Continue = “…”, … }); will return a customized HttpResult with a location redirect, but because it’s not ServiceStack processing the Services Response it, wont redirect automatically.