-3
私はいくつかのデータを引き出し、別のメソッドで使用するためにグローバル変数に割り当てる非同期メソッドがあります。変数GNAMEとgEmailは既に宣言されているasyncメソッドからグローバル変数を代入するC#
private async void getgoogleplususerdataSer(string access_token)
{
try
{
HttpClient client = new HttpClient();
var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token;
client.CancelPendingRequests();
HttpResponseMessage output = await client.GetAsync(urlProfile);
if (output.IsSuccessStatusCode)
{
string outputData = await output.Content.ReadAsStringAsync();
GoogleUserOutputData serStatus = JsonConvert.DeserializeObject<GoogleUserOutputData>(outputData);
if (serStatus != null)
{
gName = serStatus.name;
gEmail = serStatus.email;
}
}
}
catch (Exception ex)
{
//catching the exception
}
}
: は、以下の方法です。 私は次のロジックを使用してユーザーを登録するには、変数を使用したい:
以下protected void RegisterGoogleUser()
{
string strCon = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
string sql5 = "SELECT Email FROM Members where [email protected]";
using (SqlConnection con5 = new SqlConnection(strCon))
{
using (SqlCommand cmd5 = new SqlCommand(sql5, con5))
{
con5.Open();
cmd5.Parameters.AddWithValue("@gEmail", gEmail);
Object result = cmd5.ExecuteScalar();
con5.Close();
if (result != null)
{
lblMessage.Text = "This e-mail is already registered. If you forgot your password, use forgot password link to recover it.";
return;
}
else
{
string providerName = "Google";
string conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
string query = "INSERT INTO Members(Name, Email, ProviderName)values(@gName,@gEmail,@providerName)";
using (SqlConnection connection = new SqlConnection(conn))
{
using (SqlCommand cmd1 = new SqlCommand(query, connection))
{
cmd1.Parameters.AddWithValue("@gName", gName);
cmd1.Parameters.AddWithValue("@gEmail", gEmail);
cmd1.Parameters.AddWithValue("@providerName", providerName);
connection.Open();
cmd1.ExecuteNonQuery();
Session.Add("GEmail", gEmail);
Session.Add("CurrentUserName", gName);
Response.Redirect("ExternalRegistration.aspx");
connection.Close();
}
}
}
}
}
}
がGoogleUserOutputDataクラスであり、Googleのログイン方法
public class GoogleUserOutputData
{
public string id { get; set; }
public string name { get; set; }
public string given_name { get; set; }
public string email { get; set; }
public string picture { get; set; }
}
protected void btnGoogleLogin_Click(object sender, System.EventArgs e)
{
var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + googleplus_redirect_url + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + googleplus_client_id;
Session["Provider"] = "Google";
Response.Redirect(Googleurl);
}
私は取得しています例外がある:「パラメータ化クエリ「( @gEmail nvarchar(4000))EmailからのSELECT Email From Email = @ g 'は、' @ gEmail 'パラメータが指定されていません。
だから、問題は何ですか?コンパイル時エラーまたは実行時エラーが発生しますか?もしそうなら、それを述べてください。 – user3185569
まず、これは間違っています: 'catch(Exception ex){}'。 *例外のあるものを実行します。何か他には、何が間違っているのか分からない。変数を設定していませんか?例外が発生しましたか? –
また、 'async void'を使用しないでください。これはイベントハンドラでない限り' async Task'を使用してください... –