ServiceStack:在与 Google 进行身份验证时从身份验证会话中获取电子邮件 [英] ServiceStack: Get email from auth session when authenticating with Google

查看:74
本文介绍了ServiceStack:在与 Google 进行身份验证时从身份验证会话中获取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 GoogleOpenIdOAuthProvider 对用户进行身份验证.我需要访问登录用户的电子邮件地址.我试图实现 Using Typed Sessions在 ServiceStack 代码中保持原样.

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; }
}

该服务具有 [Authenticate] 属性.在我的 AppHost 设置中,我配置了这样的身份验证:

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;

然而,电子邮件总是null.如何访问此值?

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

推荐答案

您需要从 AuthProvider 中提取数据并在 CustomUserSession 中设置值.SocialBootstrapApi 示例中显示了一个示例

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

覆盖 OnAuthenticated,找到 GoogleOpenIdOAuthProvider 以访问电子邮件地址.

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

另一个示例显示在 ServiceStack OAuth - 注册而不是登录

这篇关于ServiceStack:在与 Google 进行身份验证时从身份验证会话中获取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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