Save windows id Authentication

Asked

Viewed 165 times

1

I have an application whose authentication is via windows login. With this class I get user login:

public static class UserDetails
{
    public static string GetLogin(string userName)
    {
        string login = userName.Substring(userName.IndexOf(@"\") + 1);

        return login;
    }

In this control I catch the logged in user and through the corresponding view I load all the user information as soon as it enters the program.

        public ActionResult Index()
    {
        var login = UserDetails.GetLogin(User.Identity.Name);
        var perfil = db.Perfis.FirstOrDefault(x => x.Login == login);
        if (perfil == null)
        {
            return HttpNotFound();
        }
        ViewBag.Id = perfil.Id;
        return View(perfil);
    }

Well, I have a view whose logged-in user registers information pertaining to himself as tasks he performed and date. However, as the system is, in the task registration screen the user needs to select in a dropdownlist the number of his 'Login' which is the foreign key of my table Profile to assign to him the information that is registering in the bank. How do I save this Pk_login so that the user does not have to select the login every time to enter information since automatically when he enters the system the system already recognizes the windows authentication? I’ve researched cookie and Session options but preferred another way, a way to include tasks in this same screen, rather than appearing a dropdownlist with all the logins for the user to select his, appear only his, or better yet, take his but hide from the screen and just point to the bank.

My view where I want to hide Profile.Login

 <div class="modal-body">
    <div class="row">
        <div class="col-md-12">
            @Html.LabelFor(model => model.Tarefa)
            @Html.TextBoxFor(model => model.Tarefa, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Tarefa)
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            @Html.LabelFor(model => model.Inicio)
            @Html.TextBoxFor(model => model.Inicio, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Inicio)
        </div>
        <div class="col-md-3">
            @Html.LabelFor(model => model.Conclusão)
            @Html.TextBoxFor(model => model.Conclusão, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Conclusão)
        </div>

            <div class="col-md-3">
            @Html.LabelFor(model => model.Perfil)
            @Html.DropDownListFor(model => model.Login, null, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Login)
        </div>

    </div>

If the only or best option is to create a cookie or Session, what is the simplest way to create?

1 answer

2

I would do so:

    <div class="col-md-3">
        @Html.LabelFor(model => model.Perfil)
        @ViewBag.Id
        @Html.HiddenFor(model => model.Login)
        @Html.ValidationMessageFor(model => model.Login)
    </div>

Or else I wouldn’t even write the Hidden, since you use a static class to get the login of the logged in user. This information doesn’t even need to appear in the View.


EDIT

I don’t know what yours is Action, but I imagine it’s something like the code below, in which I’ll put what you need to save properly:

[HttpPost]
public ActionResult Create(Modelo modelo)
{
    var login = UserDetails.GetLogin(User.Identity.Name);
    var perfil = db.Perfis.FirstOrDefault(x => x.Login == login);
    modelo.Perfil = perfil;

    if (ModelState.IsValid())
    {
        db.Modelos.Add(modelo);
        db.SaveChanges();

        return RedirectToAction("Index", "Modelo");
    }

    // Carregue aqui as ViewBags

    return View(perfil);
}
  • No. It made the same mistake as the other things I had tried " The Insert statement conflicted with the FK constraint... "

  • Ah, so the point is you’re having an error with FK at the time of inserting. That’s it?

  • That. I’m only able to enter the login, which is my FK, manually. I want it to be inserted to fk without the user having to type it.

  • Good! Got it! Thanks for the help!

Browser other questions tagged

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