c# - Get User Info after successful authentication using Windows Broker Authentication in Windows Metro App -
i working on windows broker authentication.i can authenticate in calling app , can comeback home page after authentication.
i not able user info (username) .i have tried 1 message written below.
a first chance exception of type 'system.exception' occurred in mscorlib.dll winrt information: response status code not indicate success: 401 (unauthorized). additional information: unauthorized (401). response status code not indicate success: 401 (unauthorized). if there handler exception, program may safely continued.
i have written code below.please friends me.
private const string resource_name ="id_token"; public async task<userinfo> getname(string accesstoken) { try { var client = new httpclient(); client.defaultrequestheaders.authorization = new windows.web.http.headers.httpcredentialsheadervalue("oauth", accesstoken); var result = await client.getstringasync(new uri(loginuri)); var profileinformation =jsonobject.parse(result).getobject(); var name = profileinformation.getnamedstring("username"); return new userinfo { name = name }; } catch (jsonexception ex) { throw new jsonexception(ex.message); } } private async void btnhomelogin_click(object sender, routedeventargs e) { string scope = "openid profile"; var client = new oauth2client(new uri(loginuri)); var starturi = client.createauthorizeurl( clientid, resource_name, scope, redirecturi, state, nonce); string authresult; try { var webauthenticationresult = await webauthenticationbroker.authenticateasync(webauthenticationoptions.none, new uri(starturi),new uri(redirecturi)); switch (webauthenticationresult.responsestatus) { case windows.security.authentication.web.webauthenticationstatus.success: //successful authentication. authresult = webauthenticationresult.responsedata.tostring(); userinfo userinfo = await getname(resource_name); break; case windows.security.authentication.web.webauthenticationstatus.errorhttp: //http error. authresult = webauthenticationresult.responseerrordetail.tostring(); break; default: //other error. authresult = webauthenticationresult.responsedata.tostring(); break; } } catch (exception ex) { //authentication failed. handle parameter, ssl/tls, , network unavailable errors here. authresult = ex.message; } }
if using above code, based on above, calling getname function constant string (resource_name) instead of actual authresult (accesstoken) returned webauthenticationresult. if intention of calling webauthenticationbroker access token should later used httpclient, need adjust code accordingly , make use of right access token when calling httpclient code. otherwise 401 not unexpected if not passing right token.