Send External Login Data to Controller

1

I need to login through Facebook, I have the login page, which renders a @Html.Partial _LoginExternal Follow the code of the two:

@using EuVotoAf.Models
@model LoginViewModel
@{
    ViewBag.Title = "Log in";
}


<div id="loginbox" style="margin-top:50px" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">

    <div class="panel panel-info">
        <div class="panel-heading">
            <div class="panel-title">Entrar</div>
            <div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="#">Forgot password?</a></div>
        </div>

        <section id="loginForm" style="padding-top:30px" class="panel-body">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                    <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Email" })
                    </div>

                    <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                        @Html.TextBoxFor(m => m.Password, "", new { @class = "form-control", @placeholder = "Senha" })
                    </div>
                    <div class="input-group">
                        <div class="checkbox">
                            <label>
                                @Html.CheckBoxFor(m => m.RememberMe, new { @type = "checkbox", @name = "remember", @value = "1" })
                                @Html.LabelFor(m => m.RememberMe)
                            </label>
                        </div>
                    </div>
                    <div style="margin-top:10px" class="form-group">
                        <!-- Button -->

                        <div class="col-sm-12 controls">
                            <input type="submit" value="Log in" class="btn btn-success" /> <br />

                            <div style="padding-top:2px">
                                <section id="socialLoginForm">
                                    @Html.Partial("_LoginExternal", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
                                </section>
                            </div>
                        </div>
                    </div>


                    <div class="form-group">
                        <div class="col-md-12 control">
                            <div style="border-top: 1px solid#888; padding-top:15px; font-size:85%">
                                Não possui uma conta ? @Html.ActionLink("Registrar", "Register")
                            </div>
                        </div>
                    </div>
            }
        </section>
    </div>
</div>

_LoginExternal

@model EuVotoAf.Models.ExternalLoginListViewModel
@using Microsoft.Owin.Security

@{
    var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
    using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = Model.ReturnUrl }))
    {
        @Html.AntiForgeryToken()
        <div id="socialLoginList">
            <p>
                @foreach (AuthenticationDescription p in loginProviders) { 
                    <button type="submit" class="btn btn-primary" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
                }
            </p>
        </div>
    }
}

However, when you click the submit button, _LoginExternal nothing happens, and the method does not even enter controller that @Html.BeginForm is linked. I tested it and made sure that authentication is correct. It loads the data in the snippets:

@{
    var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
    using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = Model.ReturnUrl }))
    {
        @Html.AntiForgeryToken()
        <div id="socialLoginList">
            <p>
                @foreach (AuthenticationDescription p in loginProviders) { 
                    <button type="submit" class="btn btn-primary" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
                }
            </p>
        </div>
    }
}

And does not send to AccountController

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    // Request a redirect to the external login provider
    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}

What in this code is wrong, or missing?

    
asked by anonymous 25.01.2017 / 13:46

1 answer

0

With the help of Jbueno , I got the following response.

Well, this is a case of specifying HTML where it is not possible to have form inside another.

What needs to be done here is simply to change the location where it is declared to div which is rendered from @Html.Partial

<div style="padding-top:2px">
  <section id="socialLoginForm">
    @Html.Partial("_LoginExternal", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
  </section>
</div>

That is, Html of the login page should look something like this:

@using EuVotoAf.Models
@model LoginViewModel
@{
    ViewBag.Title = "Log in";
}


<div id="loginbox" style="margin-top:50px" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">

    <div class="panel panel-info">
        <div class="panel-heading">
            <div class="panel-title">Entrar</div>
            <div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="#">Forgot password?</a></div>
        </div>

        <section id="loginForm" style="padding-top:30px" class="panel-body">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                    <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Email" })
                    </div>

                    <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                        @Html.TextBoxFor(m => m.Password, "", new { @class = "form-control", @placeholder = "Senha" })
                    </div>
                    <div class="input-group">
                        <div class="checkbox">
                            <label>
                                @Html.CheckBoxFor(m => m.RememberMe, new { @type = "checkbox", @name = "remember", @value = "1" })
                                @Html.LabelFor(m => m.RememberMe)
                            </label>
                        </div>
                    </div>
                    <div style="margin-top:10px" class="form-group">
                        <!-- Button -->

                        <div class="col-sm-12 controls">
                            <input type="submit" value="Log in" class="btn btn-success" /> <br />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12 control">
                            <div style="border-top: 1px solid#888; padding-top:15px; font-size:85%">
                                Não possui uma conta ? @Html.ActionLink("Registrar", "Register")
                            </div>
                        </div>
                    </div>
            }
            <div style="padding-top:2px">
                <section id="socialLoginForm">
                    @Html.Partial("_LoginExternal", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
                </section>
            </div>
        </section>
    </div>
</div>

This will allow you to login normally. Of course, as long as you meet the requirements to use externo login.

    
26.01.2017 / 17:23