0
votes

I came across few resources in internet where google authentication is done through web api.

So , I wanted to try the same in asp.net mvc web app in similar way.I am stuck in the middle on how to fetch the redirectUri for my application.

1.App is registered in google, clientid and secret is mentioned in Authconfig.cs.

2.Upon click of my button i have to redirect to GoogleSignIn page.The redirectUrl(authrequestUrl) which will take the user to signin page is something i am not getting.

In Web api,requesting below url(GET request) will return the redirectUrl as response.
http://localhost:xxxx/api/Account/ExternalLogin?returnUrl=%2f&generateState=true

which i can use for redirecting the user to google sign in page upon click of my sign in button from my Login View.

MyLogin.cshtml

<body>
    <row>
    <button id="GoogleBtn" type="button" class="col-md-5 btn">Google</button>
    </row>
</body>
<script type="text/javascript">
$document.ready(function ()
{
    $('#GoogleBtn').click(function ()
    {
        window.location.href="RedirectUrl"
    });
});
</script>

In Asp.net MVc, there exists two actions - external login and externallogincallback

Calling external login- http://localhost:49837/Account/ExternalLogin?provider=Google&returnUrl=%2f will return Error -'The required anti-forgery form field "__RequestVerificationToken" is not present.'

Calling externallogincallback- http://localhost:49837/Account/ExternalLoginCallback?provider=Google&returnUrl=%2f&generateState=true will redirect to another View (~/Views/Account/Login), where it contains a button for Google. Upon clicking that button it takes me to Google SignInPage with all request parameters to process and sign in.

I want my app to redirect directly to Google SignIn page without going to account/login view and then being redirected to SignIn page upon clicking Google Button.

Someone help me in how to fetch the redirectUrl for my mvc application.

Update:

The Oauth request url/Google sign in request url is what i had been looking for,I got confused with the redirectUrl. I found a way to build the request Url and i successfully made the call using javascript.

2
Redirect URI is the page that google will return the authentication to. It is set in Google developer console for your credentials. It is not something you fetch. - DaImTo
Thanks for taking time and responding. I was actually trying to get the RequestUrl to perform oath request to Google Signin, which i mentioned as redirectUrl throughout my question. - kaarthick raman
I am just wondering why you are useing JavaScript instead of the official Google. Net client library. - DaImTo

2 Answers

0
votes

This code lets me make the Oauth request to Google SignIn page.

MyLogin.cshtml

    <row>
         <button id="GoogleBtn" type="button" class="col-md-5 btn btn-danger" onclick="googleLogin()">Google</button>
    </row>

<script 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 = 'xxxxxxxxxxxmyclientidxxxxx.apps.googleusercontent.com';
   var REDIRECT = 'http://myapp.local/dashboard';
   var LOGOUT = 'http://myapp.local/logout';
   var TYPE = 'token';
   var _url = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE;

    function googleLogin()
    {
     window.location = _url;
    }

</script>
0
votes

Hi you can access redirect url using below code

<script>
$(document).ready(function() {
    if (location.hash) {
        if (location.hash.split('access_token')) {
            var access_Token = location.hash.split('access_token=') 
            [1].split('&')[0];
            sessionStorage.setItem("access_Token", access_Token)
        }
    }
});