استفاده از jwt در زامارین فرم
با سلام و خسته نباشید خدمت اساتید محترم
من جهت استفاده از jwt یک web api از نوع asp.net core 2.2 ایجاد کردم و با تنظیماتی که انجام دادم این api با استفاده از صفحه html و کدهای javascript و ajax به درستی کارمیکنه و توکن مورد نظر روتولید میکنه ولی اشکال کار اینجاست که وقتی از xamarin forms برای Authentication استفاده میکنم جواب نمیده خیلی راهها رو امتحان کردم اتصال شبکه رو هم بررسی کردم اشکالی نداره و از امولاتور به وب سایت دسترسی وجود داره
ضمنا من از iis express استفاده میکنم و قبلا با استفاده از asp.net api این مشکل رو نداشتم و به راحتی توکن مربوطه ایجاد میشد . لطفا راهنمایی کنید خیلی ضروریه
کدی که در زامارین استفاده میکنم :
public async Task<string> LoginUserAsync(string username, string password)
{
try
{
HttpClient client = new HttpClient();
List<KeyValuePair<string, string>> keyValue = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("username",username),
new KeyValuePair<string, string>("password",password),
new KeyValuePair<string, string>("grant_type","password")
};
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Post, Variables.path + "api/auth/login")
{
Content = new FormUrlEncodedContent(keyValue)
};
HttpResponseMessage response = await client.SendAsync(request);
string jwt = await response.Content.ReadAsStringAsync();
JObject jwtDynamic = JsonConvert.DeserializeObject<dynamic>(jwt);
string accessToken = jwtDynamic.Value<string>("token");
DateTime accessTokenExpiration = jwtDynamic.Value<DateTime>(".expires");
Settings.AccessTokenexpiartion = accessTokenExpiration;
Debug.WriteLine(jwt);
return accessToken;
}
catch (Exception)
{
return null;
}
نقل قول: استفاده از jwt در زامارین فرم
با سلام خدمت تمامی دوستداران .net
خودم اشکال رو برطرف کردم ویرای استفاده بقیه کد رو به اشتراک میزارم
public async Task<string> LoginUserAsync(string username, string password)
{
try
{
string baseUrl = Variables.path;
HttpClient client = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue
("application/json");
client.DefaultRequestHeaders.Accept.Add(contentTyp e);
User userModel = new User
{
userName = username,
Password = password
};
string stringData = JsonConvert.SerializeObject(userModel);
StringContent contentData = new StringContent(stringData,
System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response =await client.PostAsync
("api/auth/login", contentData);
string stringJWT = response.Content.
ReadAsStringAsync().Result;
JObject jwtDynamic = JsonConvert.DeserializeObject<dynamic>(stringJWT);
string accessToken = jwtDynamic.Value<string>("token");
DateTime accessTokenExpiration = jwtDynamic.Value<DateTime>(".expires");
Settings.AccessTokenexpiartion = accessTokenExpiration;
Debug.WriteLine(stringJWT);
return accessToken;
}
catch (Exception)
{
return null;
}
}
نقل قول: استفاده از jwt در زامارین فرم
نقل قول:
نوشته شده توسط
EBRAHIMIEKOKDARAGHI
با سلام خدمت تمامی دوستداران .net
خودم اشکال رو برطرف کردم ویرای استفاده بقیه کد رو به اشتراک میزارم
public async Task<string> LoginUserAsync(string username, string password)
{
try
{
string baseUrl = Variables.path;
HttpClient client = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue
("application/json");
client.DefaultRequestHeaders.Accept.Add(contentTyp e);
User userModel = new User
{
userName = username,
Password = password
};
string stringData = JsonConvert.SerializeObject(userModel);
StringContent contentData = new StringContent(stringData,
System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response =await client.PostAsync
("api/auth/login", contentData);
string stringJWT = response.Content.
ReadAsStringAsync().Result;
JObject jwtDynamic = JsonConvert.DeserializeObject<dynamic>(stringJWT);
string accessToken = jwtDynamic.Value<string>("token");
DateTime accessTokenExpiration = jwtDynamic.Value<DateTime>(".expires");
Settings.AccessTokenexpiartion = accessTokenExpiration;
Debug.WriteLine(stringJWT);
return accessToken;
}
catch (Exception)
{
return null;
}
}
سلام ممنون از راهنمایتتون منم میخواستم jwt راه اندازی کنم با کد شما راحت تر انجامش میدم