强制用户在 mvc c# 中使用其 Google 组织 (G Suite) 帐户登录
Forcing user to sign in with their Google Organization (G Suite) account in mvc c#
我在我的 mvc 站点中实施了 google 身份验证。这是我的示例代码-
AuthConfig.cs
public static class AuthConfig
{
private static string GoogleClientId = ConfigurationManager.AppSettings["GoogleClientId"];
private static string GoogleClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"];
public static void RegisterAuth()
{
GoogleOAuth2Client clientGoog = new GoogleOAuth2Client(GoogleClientId, GoogleClientSecret);
IDictionary<string, string> extraData = new Dictionary<string, string>();
OpenAuth.AuthenticationClients.Add("google", () => clientGoog, extraData);
}
}
Global.asax
AuthConfig.RegisterAuth();
AccountController.cs
public ActionResult RedirectToGoogle()
{
string provider = "google";
string returnUrl = "";
return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
}
[AllowAnonymous]
public ActionResult ExternalLoginCallback(string returnUrl)
{
string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest();
if (ProviderName == null || ProviderName == "")
{
NameValueCollection nvs = Request.QueryString;
if (nvs.Count > 0)
{
if (nvs["state"] != null)
{
NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]);
if (provideritem["__provider__"] != null)
{
ProviderName = provideritem["__provider__"];
}
}
}
}
GoogleOAuth2Client.RewriteRequest();
var redirectUrl = Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl });
var retUrl = returnUrl;
var authResult = OpenAuth.VerifyAuthentication(redirectUrl);
string ProviderDisplayName = OpenAuth.GetProviderDisplayName(ProviderName);
if (authResult.IsSuccessful)
{
string ProviderUserId = authResult.ProviderUserId;
}
return Redirect(Url.Action("Index", "User"));
}
此代码运行良好。但我想限制用户使用 his/her 组织帐户登录,例如 "abc@example.com"
。我在哪里可以指定托管域 属性?当我从 google 开发控制台为此应用程序创建应用程序 ID 和密码时,我看到了 Verify domain
选项卡。我需要在此处添加我的组织域吗?
你可以。您可以在身份验证 URI 参数中指定 hd(托管域)参数。
hd - OPTIONAL - hd(托管域)参数简化了 G Suite 托管帐户的登录过程。通过包含 G Suite 用户的域(例如,mycollege.edu),您可以指示应针对该域中的帐户优化帐户选择 UI。要优化 G Suite 帐户而不只是一个域,请使用星号:hd=*。
不要依赖此 UI 优化来控制谁可以访问您的应用程序,因为可以修改 client-side 请求。请务必验证返回的 ID 令牌是否具有符合您预期的高清声明值(例如 mycolledge.edu)。与请求参数不同,ID 令牌声明包含在来自 Google 的安全令牌中,因此该值是可信的。
我在我的 mvc 站点中实施了 google 身份验证。这是我的示例代码-
AuthConfig.cs
public static class AuthConfig
{
private static string GoogleClientId = ConfigurationManager.AppSettings["GoogleClientId"];
private static string GoogleClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"];
public static void RegisterAuth()
{
GoogleOAuth2Client clientGoog = new GoogleOAuth2Client(GoogleClientId, GoogleClientSecret);
IDictionary<string, string> extraData = new Dictionary<string, string>();
OpenAuth.AuthenticationClients.Add("google", () => clientGoog, extraData);
}
}
Global.asax
AuthConfig.RegisterAuth();
AccountController.cs
public ActionResult RedirectToGoogle()
{
string provider = "google";
string returnUrl = "";
return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
}
[AllowAnonymous]
public ActionResult ExternalLoginCallback(string returnUrl)
{
string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest();
if (ProviderName == null || ProviderName == "")
{
NameValueCollection nvs = Request.QueryString;
if (nvs.Count > 0)
{
if (nvs["state"] != null)
{
NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]);
if (provideritem["__provider__"] != null)
{
ProviderName = provideritem["__provider__"];
}
}
}
}
GoogleOAuth2Client.RewriteRequest();
var redirectUrl = Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl });
var retUrl = returnUrl;
var authResult = OpenAuth.VerifyAuthentication(redirectUrl);
string ProviderDisplayName = OpenAuth.GetProviderDisplayName(ProviderName);
if (authResult.IsSuccessful)
{
string ProviderUserId = authResult.ProviderUserId;
}
return Redirect(Url.Action("Index", "User"));
}
此代码运行良好。但我想限制用户使用 his/her 组织帐户登录,例如 "abc@example.com"
。我在哪里可以指定托管域 属性?当我从 google 开发控制台为此应用程序创建应用程序 ID 和密码时,我看到了 Verify domain
选项卡。我需要在此处添加我的组织域吗?
你可以。您可以在身份验证 URI 参数中指定 hd(托管域)参数。
hd - OPTIONAL - hd(托管域)参数简化了 G Suite 托管帐户的登录过程。通过包含 G Suite 用户的域(例如,mycollege.edu),您可以指示应针对该域中的帐户优化帐户选择 UI。要优化 G Suite 帐户而不只是一个域,请使用星号:hd=*。
不要依赖此 UI 优化来控制谁可以访问您的应用程序,因为可以修改 client-side 请求。请务必验证返回的 ID 令牌是否具有符合您预期的高清声明值(例如 mycolledge.edu)。与请求参数不同,ID 令牌声明包含在来自 Google 的安全令牌中,因此该值是可信的。