如何使用 g+ asp.net 网络表单对用户进行身份验证

How to authenticate users using g+ asp.net webform

我的网站使用 DotNetOpenAuth 通过 Gmail 帐户对用户进行身份验证。在 login.aspx.cs 中,我使用用户的电子邮件来检查他们在数据库中的状态。

我需要将其更新为 google-plus,但我还没有找到任何使用 DotNetOpenAuth 的示例。我能够使用我在此处找到的说明获取用户的电子邮件:https://developers.google.com/+/web/signin/add-button

我发现的示例建议使用隐藏的表单字段,但我知道这不安全。你能给我指出一些执行上述操作的例子,或者推荐另一种方法吗?提前致谢。

尝试使用 Google+ quickstart for .NET。这个示例应用做了两件事:

  1. 使用 Javascript 客户端授权客户端反对 Google APIs.
  2. 将授权代码传递给服务器以授权服务器进行后端 API 调用。

您应该可以从那里开始,作为授权基础知识的示例。

对于身份验证,您将需要检索 ID 令牌和 pass that ID token from the client to the server to authenticate the user

我通过访问 Google 开发人员控制台并设置项目解决了这个问题。 我配置了凭据、客户端 ID 密钥和 OAuth 同意屏幕。 接下来我将这些行添加到我的登录页面。基本上,我获取用户的 gmail 地址并将其传递到我的数据库以确认他们的权限。

            <!--this is my google plus button-->
            <fieldset style="padding-left: 12px; width: 526px;">
                <legend style="font-family: Tahoma, Geneva, Verdana, Arial, Sans-Serif; font-size: small; font-weight: normal; ">
                    Welcome to my web app...
                </legend>
                    <br />
                <span id="signinButton">
                    <span
                    class="g-signin"
                    data-callback="LoginCallback"
                    data-clientid="client id key goes here"
                    data-cookiepolicy="single_host_origin"
                    data-requestvisibleactions="http://schema.org/AddAction"
                    data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email">
                    </span>
                </span>
                <br />
            </fieldset>

        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
            <script language="javascript" type="text/javascript">
                function LoginCallback(authResult){
                    if (authResult['status']['signed_in']){
                        // get user's email address
                        gapi.client.load('oauth2', 'v2', function (){
                            gapi.client.oauth2.userinfo.get().execute(function (resp){
                                var email = resp.email;
                                // call codebehind webmethod to validate user email in DB                                    
                                PageMethods.AuthenticateMember(email, MemberPass(email), MemberFail);
                            });
                        });
                    }
                    else{
                        console.log('Sign-in state: ' + authResult['error']);
                    }
                }