As Asked Question1, Question2 from other users but there is no any proper answer got so I asked here.
I am using ASP.NET MVC and trying to load social logins providers by partial view.
But I can't it gives me an error.
This is my code from where I return my partial view:
public async Task<PartialViewResult> GetProviders()
{
string apiUrl = "mywebsite";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("Account/externalloginpathhere");
//var result = Task.Run(async () => { await client.GetAsync("Account/externalloginpathhere"); }).Result;
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
var providers = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ViewModel>>(data);
foreach(var provider in providers)
{
if (provider.Name == "Google")
{
//code come here
}
else if (provider.Name == "Facebook")
{
//code come here
}
else if (provider.Name == "Microsoft")
{
//code come here
}
}
return PartialView("~/Views/Account/_ExternalLoginsListPartial.cshtml", providers);
}
return null;
}
}
View calling from Home Controller View:
@{Html.RenderAction("GetProviders", "Account");}
This is what I have done please correct me whereever I goes wrong!
I have just figured it out don't know correct or not but I got the solution hope it helps someone.
I have just created one partial view and bind that partialview inside the page where I want login or signup process.
Bind the partial view:
@Html.Partial("~/Views/Account/_ExternalLoginsListPartial.cshtml")
External Providers Partial View:
@{
var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
if (loginProviders.Count() >= 0)
{
using (Html.BeginForm("ExternalLogin", "Account", FormMethod.Post, new { id = "externalLogin" }))
{
@Html.AntiForgeryToken()
<ul>
@foreach (var p in loginProviders)
{
<li>
<button value="@p.Caption" title="Login using @p.Caption account" name="provider" id="@p.Caption.ToLower()" type="submit" class="social-button">
<i class="fa fa-@p.Caption.ToLower()" id="@p.Caption"></i>
</button>
</li>
}
</ul>
}
}
}