Intermittent ASP.NET oAuth issue with Google, AuthenticationManager.GetExternalIdentityAsync is returning null

后端 未结 7 1572
北海茫月
北海茫月 2020-12-29 23:37

I am trying to fix an intermittent issue when using Google as an external login provider.

When attempting to login, the user is redirected back to the login page ra

相关标签:
7条回答
  • 2020-12-30 00:22

    Tom I am using google-oauth in my asp.net application by using REST API. it is working fine and i am not facing any connection issues.

    The following steps i am performing:

    1.I have created one project in google developer console in that i have created settings "Client ID for web application" which will contains the following parameters.

    a)Client ID => It will be automatically generated by google b)Email address=> It will be automatically generated by google c)Client secret=> It will be automatically generated by google d)Redirect URIs => Need to specify url of web page which will be used to handle authentication process. In this page we can authenticate and we can get user's basic information.

    my url: "http://localhost:1822/WebForm1.aspx/code"
    

    My Usage:

    1. I have created one sample project which will contains "Webpage1.aspx" and "Webpage2.aspx".

    I have set "Webpage2.aspx" startup page and I am forming open auth url in the "Webpage2.aspx" and redirecting to google login page.

    Google Open Auth url Formation

    After login, it will redirect to "Webpage1.aspx" along with access code. By passing this access code to "https://accounts.google.com/o/oauth2/token" url, i am getting access token along with token type and token expiry time. After that by passing this access to the "https://www.googleapis.com/oauth2/v2/userinfo" url, i am getting user basic information like "email,Name, Gender, Photo, etc...)

    Example Code

        public class GoogleAuthorizationData
        {
            public string access_token { get; set; }
            public int expires_in { get; set; }
            public string token_type { get; set; }
    
        }
    
      public class GoogleUserInfo
        {
            public string name { get; set; }
            public string family_name { get; set; }
            public string gender { get; set; }
            public string email { get; set; }
            public string given_name { get; set; }
            public string picture { get; set; }
            public string link { get; set; }
            public string id { get; set; }
    
        }
    
      Webpage1.aspx
      ============
     protected void Page_Load(object sender, EventArgs e)
            {
                string code = Request.QueryString["code"].ToString();
                string scope = Request.QueryString["scope"].ToString();
                string url = "https://accounts.google.com/o/oauth2/token";
                string postString = "code=" + code + "&client_id=" + ConfigurationManager.AppSettings["GoogleClientID"].ToString() + "&client_secret=" + ConfigurationManager.AppSettings["GoogleSecretKey"].ToString() + "&redirect_uri=" + ConfigurationManager.AppSettings["ResponseUrl"].ToString() + "&grant_type=authorization_code";
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
    
                UTF8Encoding utfenc = new UTF8Encoding();
                byte[] bytes = utfenc.GetBytes(postString);
                Stream os = null;
                try
                {
                    request.ContentLength = bytes.Length;
                    os = request.GetRequestStream();
                    os.Write(bytes, 0, bytes.Length);
                }
                catch
                { }
    
                try
                {
                    HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                    Stream responseStream = webResponse.GetResponseStream();
                    StreamReader responseStreamReader = new StreamReader(responseStream);
                    var result = responseStreamReader.ReadToEnd();//
                    var json = new JavaScriptSerializer();
    
                    GoogleAuthorizationData authData = json.Deserialize<GoogleAuthorizationData>(result);
    
                    HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v2/userinfo");
                    request1.Method = "GET";
                    request1.ContentLength = 0;
                    request1.Headers.Add("Authorization", string.Format("{0} {1}", authData.token_type, authData.access_token));
                    HttpWebResponse webResponse1 = (HttpWebResponse)request1.GetResponse();
                    Stream responseStream1 = webResponse1.GetResponseStream();
                    StreamReader responseStreamReader1 = new StreamReader(responseStream1);
                    GoogleUserInfo userinfo = json.Deserialize<GoogleUserInfo>(responseStreamReader1.ReadToEnd());
                   Response.Write(userinfo.email);
    
                }
                catch (Exception eX)
                {
                    throw eX;
                }
    
    
    
    
    
            }
    
    0 讨论(0)
提交回复
热议问题