在我的网站asp.net中使用gmail api登录

时间:2015-06-02 07:13:04

标签: asp.net

我必须在我的网站上添加使用gmail帐户登录的功能。仅应使用我的域的用户进行验证,尝试使用其他域登录的用户应重定向到Google登录页面并显示消息。如果用户通过gmail成功登录,那么他也应该登录到会员表。如何在asp.net中执行此操作。这是我的代码:

<script language="javascript" type="text/javascript">
        var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
        var VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=';
        var SCOPE = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email';
        var CLIENTID = '7480000038683-rhs9hdsc31uu2avuq8avlsed5i7hk.apps.googleusercontent.com';
        var REDIRECT = 'http://localhost:0000/default.aspx?company=xyz';
        var LOGOUT = 'http://accounts.google.com/Logout';
        var TYPE = 'token';
        var HD = "ranosys.com";
        var _url = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE + '&login_hint=' + LOGINHINT + '&hd='+ HD;
        var acToken;
        var tokenType;
        var expiresIn;
        var user;
        var hd;
        var loggedIn = false;
        function login() {
            var win = window.open(_url,"_self");
            var pollTimer = window.setInterval(function () {
                try {
                    console.log(win.document.URL);
                    if (win.document.URL.indexOf(REDIRECT) != -1) {
                        window.clearInterval(pollTimer);
                        var url = win.document.URL;
                        acToken = gup(url, 'access_token');
                        tokenType = gup(url, 'token_type');
                        expiresIn = gup(url, 'expires_in');

                        win.close();
                        validateToken(acToken);
                    }
                }
                catch (e) {
                }
            }, 500);
        }
        function validateToken(token) {
            $.ajax(
                {
                    url: VALIDURL + token,
                    data: null,
                    success: function (responseText) {
                        getUserInfo();
                        loggedIn = true;
                        $('#loginText').hide();
                        $('#logoutText').show();
                    },
                    dataType: "jsonp"
                });
        }
        function getUserInfo() {

            debugger;
            $.ajax({
                url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + acToken,
                data: null,
                success: function (resp) {
                    user = resp;
                    var email = user.email;
                    alert(email);
                    console.log(user);
                    $('#uName').text('Welcome ' + user.name);
                    $('#imgHolder').attr('src', user.picture);
                },
                dataType: "jsonp"
            });
        }
        //credits: http://www.netlobo.com/url_query_string_javascript.html  

        function gup(url, name) {
            namename = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\#&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(url);
            if (results == null)
                return "";
            else
                return results[1];
        }
        function startLogoutPolling() {
            $('#loginText').show();
            $('#logoutText').hide();
            loggedIn = false;
            $('#uName').text('Welcome ');
            $('#imgHolder').attr('src', 'none.jpg');
        }
</script>  

0 个答案:

没有答案