Does the WebAuthenticationBroker work in Windows 8 Metro App post Release Candidate

后端 未结 1 1880
星月不相逢
星月不相逢 2020-12-20 03:08

SOLUTION My working solution can be found in the answer or in my update two.

1) Now make sure, for testing on localhost, that you have setup windows

相关标签:
1条回答
  • 2020-12-20 03:35

    The WebAuthenticationBroker simply keeps browsing until the next requested page is the one specified by the callbackUri parameter. At that point it returns the final URL to you so if you want to get anything back it needs to be encoded in that URL.

    In the ACS control panel for the relying party you need to specify a return url that is somewhere on your site. For example https://traffictheory.azurewebsites.net/federationcallback. Then create a controller to handle accept a post to that URL. The post will have a form field wresult which is some xml that will contain the token returned from ACS.

    You can then send the token back to the WebAuthenticationBroker by redirecting to https://traffictheory.azurewebsites.net/federationcallback/end?token={whatever you want to return}

    You would then need to change the usage of the authentication broker to the following:

    var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
                WebAuthenticationOptions.None,
                new Uri("https://s-innovations.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2ftraffictheory.azurewebsites.net%2f"),
                new Uri("https://traffictheory.azurewebsites.net/federationcallback/end")
            );
    
    // The data you returned
    var token = authenticateResult.ResponseData.Substring(authenticateResult.ResponseData.IndexOf("token=", StringComparison.Ordinal) + 6);
    

    My controller for handling the authentication callback post looks like this.

    public class FederationcallbackController : ApiController
    {
        public HttpResponseMessage Post()
        {
            var response = this.Request.CreateResponse(HttpStatusCode.Redirect);
            response.Headers.Add("Location", "/api/federationcallback/end?acsToken=" + ExtractBootstrapToken());
    
            return response;
        }
    
        protected virtual string ExtractBootstrapToken()
        {
            return HttpContext.Current.User.BootstrapToken();
        }
    }
    

    The BootstrapToken() extenion method is part of the wif.swt NuGet package. By default WIF doesn't save anything to the bootstrap token property you need to enable it by including the saveBootstrapTokens="true" attribute on the <service> element under <microsoft.identityModel> in your web.config. Mine looks like this:

    <microsoft.identityModel>
        <service saveBootstrapTokens="true">
            <audienceUris>
                <add value="http://localhost:3949/" />
            </audienceUris>
            <federatedAuthentication>
                <wsFederation passiveRedirectEnabled="true" issuer="https://xyz.accesscontrol.windows.net/v2/wsfederation" realm="http://localhost:3949/" reply="http://localhost:3949/" requireHttps="false" />
                <cookieHandler requireSsl="false" path="/" />
            </federatedAuthentication>
            <issuerNameRegistry type="Microsoft.IdentityModel.Swt.SwtIssuerNameRegistry, Wif.Swt">
                <trustedIssuers>
                    <add name="https://readify.accesscontrol.windows.net/" thumbprint="{thumbprint}" />
                </trustedIssuers>
            </issuerNameRegistry>
            <securityTokenHandlers>
                <add type="Microsoft.IdentityModel.Swt.SwtSecurityTokenHandler, Wif.Swt" />
            </securityTokenHandlers>
            <issuerTokenResolver type="Microsoft.IdentityModel.Swt.SwtIssuerTokenResolver, Wif.Swt" />
        </service>
    </microsoft.identityModel>
    
    0 讨论(0)
提交回复
热议问题