Passing parameters to Azure Active Directory authentication

后端 未结 3 1042
广开言路
广开言路 2021-01-01 01:31

I have an ASP.Net MVC Application, Owin, and I\'m using Azure Active Directory authentication as well.

I want to pass a parameter when the user is redirected to the

3条回答
  •  被撕碎了的回忆
    2021-01-01 01:57

    You could pass the ProjectId parameter as value for State parameter. See the sample code below:

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    RedirectToIdentityProvider = context =>
                        {
                            redirectUri = string.Format("{0}/", System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority));
                            postLogoutRedirectUri = redirectUri + "sign-out";
                            context.ProtocolMessage.RedirectUri = redirectUri;
                            context.ProtocolMessage.PostLogoutRedirectUri = postLogoutRedirectUri;
                            context.ProtocolMessage.State = "Your Project Id";
                            return Task.FromResult(0);
                        },
                    AuthorizationCodeReceived = context =>
                        {
                            var projectId = context.ProtocolMessage.State;//Retrieve the state in AuthorizationCodeReceived event.
                            return Task.FromResult(0);
                        }
                }
            };
    

    UPDATE

    Essentially State accepts a string parameter. In our project we needed to provide many values in the state. What we did there is created a pipe delimited string and pass that as state. When we receive the state back, we simply convert that into an array and use appropriate elements. Something like:

    var state = "param1|param2|...|paramx";
    

    Other thing you could do is create a state object (a simple class with some properties), serialize it as JSON, convert that in base64 string and pass that encoded string as state after properly url encoding it. When you receive back the state, you could do the reverse process, get state object back and use it's properties values.

提交回复
热议问题