如何使用g + asp.net webform对用户进行身份验证

问题描述 投票:1回答:2

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

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

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

c# asp.net webforms google-plus dotnetopenauth
2个回答
-1
投票

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

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

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

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


0
投票

我通过访问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']);
                    }
                }
© www.soinside.com 2019 - 2024. All rights reserved.