asp.net mvc 4 - Google OAuth Log In Problems In MVC 4 Application -
i facing same issue mentioned in authenticationresult.issuccessful started returning false google
i using mvc 4 web api application , migrating mvc 5 big change. else facing same issue ?
are there samples on how use dotnetopenauth.oauth2 can skip migrating dotnetopenauth.googleoauth2 in mvc 5 app.
in case still need use google openid connect in mvc 4 here code:
in authconfig.cs write:
oauthwebsecurity.registerclient(new googlecustomclient("youtgoogleapikey", "googlesecret"), "google", null);
and googlecustomclient class (based on linkedin oauth example found):
using dotnetopenauth.aspnet; using dotnetopenauth.aspnet.clients; using dotnetopenauth.oauth; using system; using system.collections.generic; using system.io; using system.net; using system.text; using system.text.regularexpressions; using system.web; using system.web.script.serialization; namespace myproject.customproviders { public class googlecustomclient : oauth2client { #region constants , fields private const string authorizationendpoint = "https://accounts.google.com/o/oauth2/auth"; private const string tokenendpoint = "https://accounts.google.com/o/oauth2/token"; private readonly string _clientid; private readonly string _clientsecret; private string absolutereturnurl = string.empty; #endregion public googlecustomclient(string clientid, string clientsecret) : base("google") { this._clientid = clientid; this._clientsecret = clientsecret; } protected override uri getserviceloginurl(uri returnurl) { stringbuilder serviceurl = new stringbuilder(); serviceurl.appendformat("{0}?", authorizationendpoint); serviceurl.append("response_type=code"); serviceurl.appendformat("&client_id={0}", this._clientid); serviceurl.append("&scope=email"); absolutereturnurl = regex.match(returnurl.tostring(), "https://(\\d|\\w|\\/|:|\\.){1,}").value; serviceurl.appendformat("&redirect_uri={0}", uri.escapedatastring(absolutereturnurl)); serviceurl.appendformat("&state={0}", regex.match(returnurl.absoluteuri, "(?<=__sid__=).*?($|&)", regexoptions.ignorecase).value); return new uri(serviceurl.tostring()); } protected override idictionary<string, string> getuserdata(string accesstoken) { var request = webrequest.create("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + uri.escapedatastring(accesstoken)); string responsetext = string.empty; using (var response = request.getresponse()) { using (var responsestream = response.getresponsestream()) { using (var reader = new streamreader(responsestream, encoding.utf8)) { responsetext = reader.readtoend(); } } } dictionary<string, string> userdata = new dictionary<string, string>(); javascriptserializer deserializer = new javascriptserializer(); dictionary<string, string> responsedata = deserializer.deserialize<dictionary<string, string>>(responsetext); userdata.add("id", responsedata["id"]); userdata.add("firstname", responsedata["given_name"]); userdata.add("lastname", responsedata["family_name"]); userdata.add("emailaddress", responsedata["email"]); userdata.add("picture", responsedata["picture"]); userdata.add("accesstoken", ""); userdata.add("alldata", responsetext); return userdata; } protected override string queryaccesstoken(uri returnurl, string authorizationcode) { stringbuilder postdata = new stringbuilder(); postdata.append("grant_type=authorization_code"); postdata.appendformat("&code={0}", authorizationcode); postdata.appendformat("&redirect_uri={0}", absolutereturnurl.endswith(returnurl.tostring(), stringcomparison.ordinalignorecase) ? httputility.urlencode(absolutereturnurl) : ""); postdata.appendformat("&client_id={0}", this._clientid); postdata.appendformat("&client_secret={0}", this._clientsecret); string response = ""; string accesstoken = ""; var webrequest = (httpwebrequest)webrequest.create(tokenendpoint); webrequest.method = "post"; webrequest.contenttype = "application/x-www-form-urlencoded"; try { using (stream s = webrequest.getrequeststream()) { using (streamwriter sw = new streamwriter(s)) sw.write(postdata.tostring()); } using (webresponse webresponse = webrequest.getresponse()) { using (streamreader reader = new streamreader(webresponse.getresponsestream())) { response = reader.readtoend(); } } javascriptserializer deserializer = new javascriptserializer(); var userdata = deserializer.deserialize<dictionary<string, string>>(response); accesstoken = (string)userdata["access_token"]; } catch (exception) { return null; } return accesstoken; } public override authenticationresult verifyauthentication(httpcontextbase context, uri returnpageurl) { string code = context.request.querystring["code"]; if (string.isnullorempty(code)) { return authenticationresult.failed; } string accesstoken = this.queryaccesstoken(returnpageurl, code); if (accesstoken == null) { return authenticationresult.failed; } idictionary<string, string> userdata = this.getuserdata(accesstoken); if (userdata == null) { return authenticationresult.failed; } string email = userdata["emailaddress"]; string id = userdata["id"]; userdata["accesstoken"] = accesstoken; return new authenticationresult(issuccessful: true, provider: this.providername, provideruserid: id, username: email, extradata: userdata); } } }
and reason oauthwebsecurity.verifyauthentication in function "externallogincallback" not going right class here workaround:
authenticationresult result = ((googlecustomclient)oauthwebsecurity.getoauthclientdata("google").authenticationclient).verifyauthentication(this.httpcontext, uri);
Comments
Post a Comment