Whitelist for ASP.NET MVC w / Google OAuth Login

Is it possible, with the Google OAuth code, a new MVC project is being created to limit the users who can create accounts.

i.e. as a standard, anyone with a Google account can create an account on the website. I want to limit the creation of an account in a pre-authorized list of email addresses - actually a white list.

Before I write any custom code, is this requirement easier in a standard, ready-made login platform?

+4
source share
1 answer

I did this by changing these two lines in the AccountController template (forgive the rough code),

Old:

            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Hometown = model.Hometown };
            var result = await UserManager.CreateAsync(user);

New

            var user = new ApplicationUser { UserName = info.Email, Email = info.Email, Hometown = model.Hometown };
            IdentityResult result = null;
            if (!allowedUsers.Contains(info.Email))
            {
                result = IdentityResult.Failed("User is not in permitted list");
            }
            else
            {
                result = await UserManager.CreateAsync(user);
            }

, ( ) OAuth Startup.Auth , Scope, info.Email null:

        app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "",
            ClientSecret = "",
            Scope = { "email" }
        });

OAuth , , Microsoft names email "wl.emails" Scope MicrosoftAccountAuthenticationOptions.

+4

Source: https://habr.com/ru/post/1598403/


All Articles