【问题标题】:Google OpenId: Accept only users of a particular companyGoogle OpenId:仅接受特定公司的用户
【发布时间】:2012-06-28 11:55:09
【问题描述】:

我正在尝试在我的应用程序中使用 open id,并且我已使用 DotNetOpenId 成功完成此操作。

现在,Google 为公司域名下的电子邮件和其他服务提供服务。 (例如 example@acompany.com)。有没有办法将身份验证范围缩小到仅限公司用户?

我知道我可以通过检查回复中的电子邮件地址来做到这一点。但我不认为这是一个好主意。如果用户未通过 acompany.com 以外的 Google 帐户进行身份验证,则更好。

请注意,我不知道 Open AuthenticationDotNetOpenId 的内部逻辑。

编辑

默认谷歌的openId请求提示https://accounts.google.com/ServiceLogin?...

我可以手动将其(在浏览器中)更改为 https://accounts.google.com/a/iit.du.ac.bd/ServiceLogin?... 并且可以正常工作(iit.du.ac.bd 是我学校的域)

我尝试使用

创建请求
        Identifier id1 = Identifier.Parse("https://www.google.com/a/iit.du.ac.bd");
        Identifier id2= Identifier.Parse("https://www.google.com/a/iit.du.ac.bd/accounts/o8/id");
        var openid = new OpenIdRelyingParty();

       IAuthenticationRequest request1 = openid.CreateRequest(id1);
       IAuthenticationRequest request2 = openid.CreateRequest(id2);

Google 的标识符是 https://www.google.com/accounts/o8/id"

编辑2

刚刚找到google-apps-openid-url

【问题讨论】:

    标签: asp.net-mvc-3 openid dotnetopenauth google-openid


    【解决方案1】:

    您对使用电子邮件地址作为过滤器的犹豫是绝对正确。跟随你的直觉。 :)

    您应该过滤 OP Endpoint。这不仅可以向您保证 Google 是提供商,而且 Google 为每个单独的域都有一个专用的 OP Endpoint,因此您可以检查。

    IAuthenticationResponse response = relyingParty.GetResponse();
    if (response != null) {
        if (response.Provider.Uri.AbsoluteUri == "http://google.com/o8/....?domain=yourcompany.com") {
            // Allow it
        } else {
            // Disallow it
        }
    }
    

    类似的东西。您必须测试以查看您所期望的情况的实际 URI 是什么。

    【讨论】:

    • 无论我从哪里获得身份验证,我都会得到相同的 AbsoluteUri。始终https://www.google.com/accounts/o8/ud。有没有办法告诉我必须从哪个特定的提供者那里进行身份验证。默认情况下,登录页面转到https://accounts.google.com/ServiceLogin...。但如果我将其更改为https://accounts.google.com/a/iit.du.ac.bd/ServiceLogin...,它就可以正常工作。但是,响应仍然来自谷歌。没有我公司的迹象(实际上是学校)。
    • 嗯...我认为 Google 为每个域使用了一个唯一的 OP 端点,类似于 https://google.com/accounts/o8/ud?hd=examplefoobar.com。如果不是,我不知道有什么方法可以保证用户属于公司/学校。无论如何,检查电子邮件地址不是保证,因为普通的 Google 帐户可能会在个人资料中列出该电子邮件地址,即使此人不再(不再)隶属于公司。跨度>
    【解决方案2】:

    要验证用户的电子邮件地址,您必须要求提供它作为某个点。在身份验证之前或在 DotNetOpenId 请求中询问。如果您只允许@abcInc.com 地址而不是其他任何人,我真的根本看不到使用 openId 的理由。您最好使用默认的 .net 会员提供程序。

    编辑:在后面添加openId代码

        [AllowAnonymous]
        [HttpPost]
        public ActionResult openIdLogin(FormCollection collection)
        {
            var openid = new OpenIdRelyingParty();
            IAuthenticationRequest aRequest = openid.CreateRequest(Identifier.Parse(collection["openid_identifier"]));
    
            string ReturnUrl = Request.Form["ReturnUrl"];
    
            if (!String.IsNullOrEmpty(ReturnUrl)) {
            aRequest.AddCallbackArguments("ReturnUrl", ReturnUrl);
            }
            var fetch = new FetchRequest();
            fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            aRequest.AddExtension(fetch);
    
            return aRequest.RedirectingResponse.AsActionResult();
        }
    
    
        [AllowAnonymous]
        public ActionResult openIdLogin(string ReturnUrl)
        {
            if (ReturnUrl == null) ReturnUrl = "";
    
            var openid = new OpenIdRelyingParty();
            IAuthenticationResponse response = openid.GetResponse();
            if (response != null)
            {
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        ClaimsResponse sreg = response.GetExtension<ClaimsResponse>();
                        if (sreg != null)
                        {
                            sreg.Email; //do something with the email address
    
                        }
                       //codez
                    break;
                    case AuthenticationStatus.Canceled:
                        ModelState.AddModelError("loginIdentifier", "Login was cancelled at the provider");
                        break;
                    case AuthenticationStatus.Failed:
                        ModelState.AddModelError("loginIdentifier", "Login failed using the provided OpenID identifier");
                        break;
                }
            }
            return View();
        }
    

    【讨论】:

    • 是的,我知道我可以通过查看电子邮件进行验证。但我不喜欢这个主意。我使用 openId 是因为我不想自己处理凭据。
    • 表单身份验证的开箱即用实现与 DotNetOpenId 一样简单。无论如何,VS 会为您完成大部分工作。据我所知,Google 并不关心您的限制,除了您可以询问(以及 Google 愿意返回)关于用户的内容。仍然由您来验证它
    • 它与简单或教无关。我习惯于处理 Forms Auth。 --------而且您认为不能应用限制? :(
    • 我不明白。在 VS 中创建的新 MVC 项目已经包含帐户页面、webconfig 设置和一个sql 脚本以针对您的数据库运行。无论哪种方式,您都必须修改身份验证过程以验证电子邮件。表单身份验证的优点是您可以进行客户端验证。
    • 但是通过表单身份验证,我需要验证正在注册的用户是有效用户。通常通过电子邮件验证完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多