1

I have a testing database on my local server. In the database, I have a table called "Korisnici" (eng. Users). Using EntityFrameworkCore I generated classes from a database, and here is generated "Korisnici" class:

public partial class Korisnici
{
    public Korisnici()
    {
        BankovniRacuni = new HashSet<BankovniRacuni>();
        Dokumenti = new HashSet<Dokumenti>();
        ObracuniZarada = new HashSet<ObracuniZarada>();
        Poslodavci = new HashSet<Poslodavci>();
        PrihodiPoslodavca = new HashSet<PrihodiPoslodavca>();
        RashodiPoslodavca = new HashSet<RashodiPoslodavca>();
        Takse = new HashSet<Takse>();
        Zaposleni = new HashSet<Zaposleni>();
    }

    public int Id { get; set; }
    public string Ime { get; set; }
    public string Prezime { get; set; }
    [Required]
    [Display(Name = "Korisnicko ime")]
    public string KorisnickoIme { get; set; }
    public string Email { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Lozinka")]
    public string Lozinka { get; set; }
    public int? TipKorisnika { get; set; }

    public virtual TipoviKorisnika TipKorisnikaNavigation { get; set; }
    public virtual ICollection<BankovniRacuni> BankovniRacuni { get; set; }
    public virtual ICollection<Dokumenti> Dokumenti { get; set; }
    public virtual ICollection<ObracuniZarada> ObracuniZarada { get; set; }
    public virtual ICollection<Poslodavci> Poslodavci { get; set; }
    public virtual ICollection<PrihodiPoslodavca> PrihodiPoslodavca { get; set; }
    public virtual ICollection<RashodiPoslodavca> RashodiPoslodavca { get; set; }
    public virtual ICollection<Takse> Takse { get; set; }
    public virtual ICollection<Zaposleni> Zaposleni { get; set; }
}

This class is used as a model for one View called "Index.cshtml":

@model Korisnici
<img src="/Content/images/LogoFinal.png" />
<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account"))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.KorisnickoIme, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.KorisnickoIme, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.KorisnickoIme, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Lozinka, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Lozinka, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Lozinka, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Uloguj se" class="btn btn-primary" />
                    </div>
                </div>
            }
        </section>
    </div>
</div>

When I click on a submit button, Login action from Controler "AccountControler" is called.

public class AccountController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public IActionResult Login(LoginViewModel model)
    {
        // Acces database and search for account
        var dbContext = new AdministracijaZingDevDBContext();
        var korisnik = dbContext.Korisnici
            .Where(k => k.KorisnickoIme == model.KorisnickoIme)
            .Where(k => k.Lozinka == model.Lozinka)
            .FirstOrDefault();

        if (korisnik != null)
        {
            HttpContext.Session.SetString("UserName" , model.KorisnickoIme);
            

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

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

I have inserted testing data in a database with one record of the Korisnici table. When I enter correct data into the LogIn form, nothing happens (the user didn't pass login).

0

1 Answer 1

1

just to check, you created migration files and updated the db and so on ?

(This should be in a comment, but I lack the reputation)

also you shouldn't create a new context, but inject it into the constructor of your account controller ( or beter still ... you should inject a repository or better use a unit of work-design and CQRS-design)

Take care and good luck.

so you type ctor tab tab

which would give you

public AccountController(){}

and than you add a parameter in the AccountController-function like

public AccountController(MyContext context){} 

right click => quick actions and refactoring => create and assign property MyContext

but you should create at least a repositorypattern and inject something like IKorisniciRepository.

It would be easier if you placed your repo on gitHub so I can test before writing.

Try

 public class AccountController : Controller
{
    private readonly AdministracijaZingDevDBContext Context {get;}

    public AccountController (AdministracijaZingDevDBContext context) {Context = context;}

    public IActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public IActionResult Login(LoginViewModel model)
    {
        // Access database and search for account
       
        var korisnik = Context.Korisnici
            .Where(k => k.KorisnickoIme == model.KorisnickoIme)
            .Where(k => k.Lozinka == model.Lozinka)
            .FirstOrDefault();

    if (korisnik != null)
    {
        HttpContext.Session.SetString("UserName" , model.KorisnickoIme);
        

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

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

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.