Send External Login Data to Controller

Asked

Viewed 139 times

1

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

@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>
    }
}

But when you click the button submit of _LoginExternal nothing happens, and the method does not even enter the controller that the @Html.BeginForm is bound. I tested 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 it doesn’t send to the 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 ?

  • Ever thought of using Identity it takes care of all authentication using facebook, twitter, github etc... Link

  • @Marcoviniciussoaresdalalba I’m using Identity, authentication is OK. Only data coming from authentication does not go to the controller informed in the question.

1 answer

0


With the help of Jbueno, I got the next answer.

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

What needs to be done in this case is simply to change the place where it is declared to div that is rendered from @Html.Partial

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

that rend. That is, the Html from the login page, it must 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 log in normally. Of course, as long as you meet the requirements to use login externo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.