Evi*_*lDr 2 asp.net asp.net-mvc owin katana google-signin
在我的网络应用程序中,我已将Google注册为单点登录提供商:
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
ClientId = "8765.......apps.googleusercontent.com",
ClientSecret = "Secret"
})
Run Code Online (Sandbox Code Playgroud)
我的应用不允许用户注册/注册(而是他们的帐户由管理员创建,但他们稍后可以将他们的帐户与Google关联).
在我的"使用Google登录"控制器中,我正在尝试发布一个Challenge()重定向到Google.这可能不是正确的方法:
string redirectUri = "http://localhost:55262/SSO/Google/ProcessToken"; // actually created in code, but shown as string for clarity
AuthenticationProperties properties = new AuthenticationProperties();
properties.RedirectUri = Server.UrlEncode(redirectUri);
Context.GetOwinContext().Authentication.Challenge(properties, "Google");
Run Code Online (Sandbox Code Playgroud)
这正确地将用户发送给Google,但Google会出现错误:redirect_uri_mismatch,说:
请求中的重定向URI:http:// localhost:55262/signin-google 与注册的重定向URI不匹配.
我在Google控制面板中的返回URI集合不包含redirect_uri指定之前看到过此错误.
如果我在VS2015中调试,我可以看到redirect_uri属性设置正确AuthenticationProperties,但似乎OWIN/Katana没有将它传递给Google.相反,当我点击Google时,return_uri是OWIN/Katana使用的默认值.我设置的那个被忽略了.
Google请求详细信息似乎证实了这一点:
scope=openid profile email
response_type=code
redirect_uri=http://localhost:55262/signin-google
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我是否应该Challenge()不允许用户将其本地应用程序帐户与Google相关联?
提供有关已接受答案的其他信息......
/signin-google由此出现的是,/signin-googleURI由OWIN/Katana内部管理.你作为一个开发者,并不需要由它来关注,但是你做需要它在谷歌开发者控制台添加为授权的重新导向URI.
在Google请求中,请注意,无论您在属性中设置了哪个自定义URI ,OWIN 始终会将重定向URI传递给Google .虽然起初这可能看起来像是一个错误/问题,但它的一个主要优点是OWIN可以通过一个回调URI来管理所有回调.您的回调URI也不会被遗忘(见下文)!/signin-googleAuthenticationProperties.RedirectUri
嗯,这就是它AuthenticationProperties()发挥作用的地方.通过指定您自己的回调网址,如此...
AuthenticationProperties properties = new AuthenticationProperties { RedirectUri = "https://my.app.com/custom/callback/uri" };
Run Code Online (Sandbox Code Playgroud)
...在OWIN检查了Google令牌并提取了必要的详细信息后,用户将被重定向到您指定的URL.
这是我感到困惑的地方,因为我不明白该怎么做/signin-google,实际上没有采取任何行动.这适用于MVC和网络表单 - 您无需关心传递给Google的内容.但是,如果使用webforms或在web.config中指定授权规则,则需要这样做以防止返回用户再次访问日志页面:
<location path="signin-google">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
以下是将用户发送给Google所需的所有代码,并返回包含其详细信息的令牌:
将用户从控制器,按钮单击事件,页面加载,任何内容(无论您的ASP /托管堆栈)发送给Google:
// set the callback, for after OWIN finishes examining what comes back from Google
AuthenticationProperties properties = new AuthenticationProperties { RedirectUri = "https://www.myapp.com/some/callback/uri" };
// send the user to Google
Context.GetOwinContext().Authentication.Challenge(properties, "Google");
// Stop execution of the current page/method - the 401 forces OWIN to kick-in and do its thing
Response.StatusCode = 401;
Response.End();
Run Code Online (Sandbox Code Playgroud)
用户在验证其身份后将从Google返回
Microsoft.AspNet.Identity.Owin.ExternalLoginInfo loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
Run Code Online (Sandbox Code Playgroud)
请注意,OWIN 的开放式身份验证具有预定义的方法。换句话说,在 中localhost:port/signin-google,OWIN 等待外部身份验证服务调用 signin -google(虽然您在项目中找不到它的实现)。在登入,谷歌是一个有效的和工作路径和我prefoundly劝你不要去改变它(由于避免编写一个新的实现作为控制器的动作)。
我也遇到过类似的问题,折腾了好几天,终于发现问题出在原用户的URL上,这个URLredirect_uri对OWIN发送的有效。清楚地:
redirect_uri等于
www.site.com/signin-googleredirect_uri等于
site.com/signin-google并且 Google 将根据在控制台中输入的重定向 URL 针对上述情况之一返回redirect_uri_mismatch 错误。我认为您的问题也来自这个现实,解决方案是在控制台中设置任何可能的 URL。
| 归档时间: |
|
| 查看次数: |
5048 次 |
| 最近记录: |