【问题标题】:Dynamically set OWIN redirect uri动态设置 OWIN 重定向 uri
【发布时间】:2018-06-15 00:46:21
【问题描述】:

我正在使用 OWIN 通过 ASP.NET MVC 应用程序中的 Microsoft Graph API 连接到 O365。一切都在 Startup.Auth.cs 中设置,包括当前来自 web.config 的 Redirect Uri 值。身份验证工作正常。

由于我在应用注册中使用通配符,重定向 uri 可以是多种值,并且用户可以从应用中的任意数量的页面向 O365 进行身份验证。一旦通过身份验证,我希望将它们带回他们刚刚所在的页面,但由于重定向 uri 已经设置,它们将被带回该页面。

在创建 OWIN 身份上下文之后,如何在我的代码的其他位置修改重定向 uri?

下面是启动代码的sn-p。

   public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
            ....                
            app.UseOpenIdConnectAuthentication(
               new OpenIdConnectAuthenticationOptions
               {

                   ClientId = appId,
                   Authority = "https://login.microsoftonline.com/organizations/v2.0",
                   PostLogoutRedirectUri = redirectUri,
                   RedirectUri = redirectUri,
                   Notifications = new OpenIdConnectAuthenticationNotifications
                   {
                       AuthorizationCodeReceived = async (context) =>
                       {

                           Dictionary<string, string> data = new Dictionary<string, string>();
                           data.Add("client_id", appId);
                           data.Add("client_secret", appSecret);
                           data.Add("code", code);
                           data.Add("grant_type", "authorization_code");
                           data.Add("redirect_uri", redirectUri);
                           ...

【问题讨论】:

    标签: c# asp.net-mvc owin


    【解决方案1】:

    我也遇到过类似的情况。我绑定到 RedirectToIdentityProvider,在将请求发送到身份提供者之前修改 RedirectUri。类似于以下内容

    Notifications = new OpenIdConnectAuthenticationNotifications()
      { 
        RedirectToIdentityProvider = async (context) =>
          {
            context.ProtocolMessage.RedirectUri = "Whatever_You_Want_Here";
          }
      }
    

    【讨论】:

    • 对我来说效果很好。我一直在设置 context.Options.RedirectUri。这解决了我的问题。
    • 非常有用的答案,谢谢。绕着圈子试图弄清楚如何从实际请求中获取值,而不是在启动时为具有多个绑定的站点。
    猜你喜欢
    • 2014-08-16
    • 1970-01-01
    • 2022-12-10
    • 2015-02-07
    • 2022-01-11
    • 2013-01-07
    • 2011-01-22
    • 2011-02-12
    • 2015-08-19
    相关资源
    最近更新 更多