Programmatically authenticate AzureAd / OpenId on an MVC controller using C # and get uri redirection

I overridden the built-in WebClient as shown below. Then I call it

public class HttpWebClient : WebClient
{
    private Uri _responseUri;

    public Uri ResponseUri
    {
        get { return _responseUri; }
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);

        _responseUri = response.ResponseUri;

        return response;
    }
}

Then I use it as follows:

using (HttpWebClient client = new HttpWebClient())
{
    client.Headers[HttpRequestHeader.Authorization] = $"Bearer { _token }";                    
    client.Headers[HttpRequestHeader.ContentType] = "application/json";
    client.UploadData(_url, Encoding.UTF8.GetBytes(_data));

    string queryString = client.ResponseUri.Query.Split('=').Last();
}

The uri response is returned with https: //login.microsoftonline ", and not the url returned from the MVC using the query string, since it authenticates first with this carrier token using AzureAd / OpenId. If I call it twice, it returns the original _url but not redirected. If I remove AzureAd authentication it works fine. Is there any way to get uri response back, how does it install MVC controller?

+4
1

, "UseOpenIdConnectAuthentication" AAD, uri, Notifications.RedirectToIdentityProvider, - :

Notifications = new OpenIdConnectAuthenticationNotifications
{
    RedirectToIdentityProvider = async _ =>
    {
        _.ProtocolMessage.RedirectUri = _.Request.Uri.ToString();
    }
}

- , , , ,

0

Source: https://habr.com/ru/post/1628339/


All Articles