Customize windows authentication

Asked

Viewed 123 times

2

I’m creating an app where I use Windows Authentication for validation of user data. As soon as the application opens, some information from that logged-in user already appears on the first page. In another menu I open all registered users.

How to click the user and open the same page that opens when I enter the application, only with the data of that user I clicked?

That’s the Index of my PerfilController which is where I get the information through windows authentication that I want to present as soon as the user logging in enters the application:

public ActionResult Index()
{
    var chapa = UserDetails.GetChapa(User.Identity.Name);
    var perfil = db.Perfis.FirstOrDefault(x => x.Chapa == chapa);
    if (perfil == null)
    {
        return HttpNotFound();
    }

    ViewBag.Id = perfil.Id;
    return View(perfil);
}

This is the view Relatorio I have where returns the list and position of the people I have registered at the bank. I wanted to click on "View Profile" and open the profile of that person I clicked on.

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Chapa)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Nome)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Cargo)
    </td>

    <td>
       @* @Html.ActionLink("Edit", "Edit", new { id=item.Id }) *@|
        @Html.ActionLink("Ver Perfil", "Details", new { id=item.Id }) |
       @* @Html.ActionLink("Delete", "Delete", new { id=item.Id })*@
    </td>
</tr>
}
  • 1

    You already have some code implemented to have a starting point?

  • public Actionresult Index() { var chapa = Userdetails.Getchapa(User.Identity.Name); var perfil = db.Perfis.Firstordefault(x => x.Chapa == chapa); if (perfil == null) { Return Httpnotfound(); } Viewbag.Id = profile. Id; Return View(profile); }

  • This is the index of my profile where I get the information I want to present as soon as the user enters the application

  • Can you put this comment in your question? Click editar just below the question and add the code. Don’t forget that to put code in the question, put 4 spaces at the beginning of the line to differentiate.

  • Okay, I put it there. I’m sorry it’s the first time I’m asking here.

  • Okay, no problem. I’ll guide you.

Show 1 more comment

1 answer

2

Make another Action, for example, Details, in his Controller:

public ActionResult Usuario(String userName)
{
    var chapa = UserDetails.GetChapa(userName);
    var perfil = db.Perfis.FirstOrDefault(x => x.Chapa == chapa);
    if (perfil == null)
    {
        return HttpNotFound();
    }

    ViewBag.Id = perfil.Id;
    return View(perfil);
}

Don’t forget to create the respective View, in the case, Details.cshtml.

To test, call at the address:

http://test address:port/Users/Details/? username=so-and-so

In the View Report, amend the following:

<td>
   @* @Html.ActionLink("Edit", "Edit", new { id=item.Id }) *@|
    @Html.ActionLink("Ver Perfil", "Details", "Usuarios", new { userName = item.Nome }) |
   @* @Html.ActionLink("Delete", "Delete", new { id=item.Id })*@
</td>
  • public class Reporiocontroller : Controller { private Datacontext db = new Datacontext(); // // GET: /Reporter/ public Actionresult Index() { var funcionarios = db.Perfis.Tolist(); Return View(funcios); } } } I have this controller where all profiles I have registered in the bank are listed for me. What I need is to click on the name or "see profile" that appears in front of each name and open for me the initial profile screen only with the data of this person that I clicked.

  • Okay, I’ll modify the answer for you.

  • @foreach (var item in Model) { <tr> <td> @Html.Displayfor(modelItem => item. Plate) </td> <td> @Html.Displayfor(modelItem => item.Name) </td> <td> @Html.Displayfor(modelItem => item.Cargo) </td> <td> @* @Html.Actionlink("Edit", "Edit", new { id=item.Id }) @| @Html.Actionlink("View Profile", "Details", new { id=item.Id }) | @ @Html.Actionlink("Delete", "Delete", new { id=item.Id })*@ </td> </tr> } the view

  • Can you please re-edit the question and enter this code as the code of the View? As a comment is not readable.

  • @Victor I edited the answer. See now.

  • Ok. I created the action, created within reports a view called Details. When I do the search gives Server Error in '/' application. What I need to put in the Details.cshtml view?

  • Is not in Relatorios, is in UsuariosController. I had the wrong answer.

  • Thank you for your patience and attention. You’re still giving the same thing. I’m making some wrong reference. Recap: I have a Profile Controller that one passed an excerpt of the above code. I also have a Reportariocontroller which is where I want to click to get the profile of any user. I added within Report the User action you informed me about. I created a Details view within report. Then in the Report View I started to have two folders: Index and Details. Inside the Index folder I changed @Html.Actionlink("View Profile"), "Details", "Users", new ( username = item.Name }) Error?

  • I discovered the error. I was changing the name of the views. Now when I compile gives an error " nullreferenceexception was unhandled by user code " pointing to the line 'string plate = username.Substring(username.Indexof(@"") + 1 ' within my Userdetails class

  • That’s another mistake, possibly involving userName, null.

  • But it’s not null... :/

  • So put the breakpoint on View and see what’s null.

Show 7 more comments

Browser other questions tagged

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