Save windows authentication id

1

I have an application whose authentication is via windows login. With this class I get the 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 get the user logged in and through the corresponding view I load all 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, in the way the system is, in the task registry screen the user needs to select in a dropdownlist the number of his 'Login' which is the foreign key of my Profile table to assign him the information he is registering with the bank. How do I save this PK_Login so that the user does not have to select the login every time it will enter an information since automatically when it enters the system the system already recognizes by the authentication of windows? I already searched the cookie and session options but preferred another way, a way in this same task inclusion screen, instead of a dropdownlist with all logins for the user to select his, appear only his, or better still , 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 it?

    
asked by anonymous 26.06.2014 / 22:02

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 you would not even type Hidden , since you use a static class to get the login of the logged in user. This information does not even need to appear in the View.

EDIT

I do not know what your Action is, but I guess it's something like the code below, where I'll put what you need to save correctly:

[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);
}
    
26.06.2014 / 22:07