动态设置OWIN重定向uri

时间:2018-05-23 12:55:04

标签: c# asp.net-mvc owin

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

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

在创建OWIN标识上下文后,如何修改代码中其他位置的重定向uri?

以下是启动代码的片段。

   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);
                           ...

1 个答案:

答案 0 :(得分:0)

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

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