ServiceStack: Get email from auth session when authenticating with Google

别等时光非礼了梦想. 提交于 2019-12-07 07:30:40

问题


I am authenticating users via GoogleOpenIdOAuthProvider. I need to access the email address of the user that logged in. I have attempted to implement the Using Typed Sessions in ServiceStack code as-is.

So, I created a base class that my service inherits from:

public abstract class AppServiceBase : Service
{
    //private CustomUserSession userSession;
    protected CustomUserSession UserSession
    {
        get
        {
            return base.SessionAs<CustomUserSession>();
        }
    }
}

public class CustomUserSession : AuthUserSession
{
    public string CustomId { get; set; }
}

The service has the [Authenticate] attribute on it. In my AppHost setup, I have configured auth like this:

        Plugins.Add(new AuthFeature(() => new CustomUserSession(),
            new IAuthProvider[] {
            new GoogleOpenIdOAuthProvider(appSettings)    //Sign-in with Google OpenId
        }));

Once the user has authenticated, the service tries to access the auth session from the base class like this:

var x = base.UserSession.Email;

However, Email is always null. How can I access this value?


回答1:


You will need to pull the data from the AuthProvider and set the value in the CustomUserSession. An example of this is shown in the SocialBootstrapApi sample

https://github.com/ServiceStack/SocialBootstrapApi/blob/master/src/SocialBootstrapApi/Models/CustomUserSession.cs#L50

Override OnAuthenticated, find the GoogleOpenIdOAuthProvider to get to the email address.

Another example is shown at ServiceStack OAuth - registration instead login



来源:https://stackoverflow.com/questions/16151740/servicestack-get-email-from-auth-session-when-authenticating-with-google

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!