How to detect if an email is a Google Account?

前端 未结 6 1491
一向
一向 2021-02-19 12:16

Me and some guys here are working on an start up. We are currently using Google OpenID API to manage registration and login to our app, but we want to migrate to a easier user r

6条回答
  •  眼角桃花
    2021-02-19 12:42

    You can check the Identity Provider with a google api https://dns.google.com/resolve?name=example.com&type=MX

        public static class IdentityProviderChecker
        {
            public static async Task GetProviderName(string email)
            {
                var domainName = email.Split('@').Last();
                using var client = new HttpClient();
    
                var result = await client.GetAsyncResult($"https://dns.google.com/resolve?name={domainName}&type=MX");
                return result.Answer.Any(x => x.Data.Contains("google.com", StringComparison.OrdinalIgnoreCase) || x.Data.Contains("googlemail.com", StringComparison.OrdinalIgnoreCase))
                    ? "Google"
                    : result.Answer.Any(x => x.Data.Contains("outlook.com", StringComparison.OrdinalIgnoreCase))
                        ? "Microsoft"
                        : null;
            }
        }
    
        public class DnsResponse
        {
            public Answer[] Answer { get; set; } = null!;
        }
    
        public class Answer
        {
            public string Data { get; set; } = null!;
        }
    
    

提交回复
热议问题