ASP.NET MVC - Custom authentication doen't work












0















I have created a simple authentication form to authenticate user using FormsAuthentication.



This is how I have used it



public ActionResult LoginUser(Login login)
{
//var encodedPassword = HashPassword.Decode(login.Password);
var encodedPassword = login.Password;
var loginData = context.Accounts.Where(p => p.Username == login.Username && p.Password == encodedPassword).SingleOrDefault();

if (loginData != null)
{
FormsAuthentication.SetAuthCookie(login.Username, false);


return RedirectToAction("Index", "Home");
}
else
{
TempData["errMess"] = "Invalid credential";
return RedirectToAction("Login");
}
}


Now I have a code to get a logged in username in _layout.cshtml



<div class="profile_info">
<span>
Welcome,
@if (Request.IsAuthenticated)
{
@Html.Encode(User.Identity.Name)
}else {
<strong>Hello worlds</strong>
}
</span>
</div>


but this doesn't solve my problem
What am I doing wrong or am I missing something please help me I am new in dot net
Thanks!










share|improve this question























  • What's your problem then? Is that the username not displayed or something else? Please explain what thing doesn't work, also consider using Membership or Identity provider instead of custom-made authentication.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:07











  • I was unable to get logged in username and also Authorize doesn't work in controller

    – Nishan
    Nov 23 '18 at 8:08











  • Do you have an auth cookie maybe????try checking if an auth cookie is created, if not , change the necessary settings in the web.config

    – mahlatse
    Nov 23 '18 at 8:15











  • @mahlatse - auth cookies is created in the format of E63C9EC8287A32E7CCB8A98B88345EBDCB0B01E2E1F0D731EECC407CCD648C8388468CD922A73C1C08C48091066E6028B25D58391DCE5E837010959ED7AC9A2176616C4985BBB80E6738EAFCE7BEA3C3E1ABB68C4927BA8C04CCCF6DC8A051168A4A9960496025C9E2C8788ADA03BC1F. How to change setting in web.config

    – Nishan
    Nov 23 '18 at 8:16











  • Make sure that client browser had enabled cookies, also use <authentication mode="Forms">. The User.Identity.Name property retrieve its value from request cookie.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:18
















0















I have created a simple authentication form to authenticate user using FormsAuthentication.



This is how I have used it



public ActionResult LoginUser(Login login)
{
//var encodedPassword = HashPassword.Decode(login.Password);
var encodedPassword = login.Password;
var loginData = context.Accounts.Where(p => p.Username == login.Username && p.Password == encodedPassword).SingleOrDefault();

if (loginData != null)
{
FormsAuthentication.SetAuthCookie(login.Username, false);


return RedirectToAction("Index", "Home");
}
else
{
TempData["errMess"] = "Invalid credential";
return RedirectToAction("Login");
}
}


Now I have a code to get a logged in username in _layout.cshtml



<div class="profile_info">
<span>
Welcome,
@if (Request.IsAuthenticated)
{
@Html.Encode(User.Identity.Name)
}else {
<strong>Hello worlds</strong>
}
</span>
</div>


but this doesn't solve my problem
What am I doing wrong or am I missing something please help me I am new in dot net
Thanks!










share|improve this question























  • What's your problem then? Is that the username not displayed or something else? Please explain what thing doesn't work, also consider using Membership or Identity provider instead of custom-made authentication.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:07











  • I was unable to get logged in username and also Authorize doesn't work in controller

    – Nishan
    Nov 23 '18 at 8:08











  • Do you have an auth cookie maybe????try checking if an auth cookie is created, if not , change the necessary settings in the web.config

    – mahlatse
    Nov 23 '18 at 8:15











  • @mahlatse - auth cookies is created in the format of E63C9EC8287A32E7CCB8A98B88345EBDCB0B01E2E1F0D731EECC407CCD648C8388468CD922A73C1C08C48091066E6028B25D58391DCE5E837010959ED7AC9A2176616C4985BBB80E6738EAFCE7BEA3C3E1ABB68C4927BA8C04CCCF6DC8A051168A4A9960496025C9E2C8788ADA03BC1F. How to change setting in web.config

    – Nishan
    Nov 23 '18 at 8:16











  • Make sure that client browser had enabled cookies, also use <authentication mode="Forms">. The User.Identity.Name property retrieve its value from request cookie.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:18














0












0








0








I have created a simple authentication form to authenticate user using FormsAuthentication.



This is how I have used it



public ActionResult LoginUser(Login login)
{
//var encodedPassword = HashPassword.Decode(login.Password);
var encodedPassword = login.Password;
var loginData = context.Accounts.Where(p => p.Username == login.Username && p.Password == encodedPassword).SingleOrDefault();

if (loginData != null)
{
FormsAuthentication.SetAuthCookie(login.Username, false);


return RedirectToAction("Index", "Home");
}
else
{
TempData["errMess"] = "Invalid credential";
return RedirectToAction("Login");
}
}


Now I have a code to get a logged in username in _layout.cshtml



<div class="profile_info">
<span>
Welcome,
@if (Request.IsAuthenticated)
{
@Html.Encode(User.Identity.Name)
}else {
<strong>Hello worlds</strong>
}
</span>
</div>


but this doesn't solve my problem
What am I doing wrong or am I missing something please help me I am new in dot net
Thanks!










share|improve this question














I have created a simple authentication form to authenticate user using FormsAuthentication.



This is how I have used it



public ActionResult LoginUser(Login login)
{
//var encodedPassword = HashPassword.Decode(login.Password);
var encodedPassword = login.Password;
var loginData = context.Accounts.Where(p => p.Username == login.Username && p.Password == encodedPassword).SingleOrDefault();

if (loginData != null)
{
FormsAuthentication.SetAuthCookie(login.Username, false);


return RedirectToAction("Index", "Home");
}
else
{
TempData["errMess"] = "Invalid credential";
return RedirectToAction("Login");
}
}


Now I have a code to get a logged in username in _layout.cshtml



<div class="profile_info">
<span>
Welcome,
@if (Request.IsAuthenticated)
{
@Html.Encode(User.Identity.Name)
}else {
<strong>Hello worlds</strong>
}
</span>
</div>


but this doesn't solve my problem
What am I doing wrong or am I missing something please help me I am new in dot net
Thanks!







asp.net-mvc authentication forms-authentication






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 8:04









NishanNishan

14914




14914













  • What's your problem then? Is that the username not displayed or something else? Please explain what thing doesn't work, also consider using Membership or Identity provider instead of custom-made authentication.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:07











  • I was unable to get logged in username and also Authorize doesn't work in controller

    – Nishan
    Nov 23 '18 at 8:08











  • Do you have an auth cookie maybe????try checking if an auth cookie is created, if not , change the necessary settings in the web.config

    – mahlatse
    Nov 23 '18 at 8:15











  • @mahlatse - auth cookies is created in the format of E63C9EC8287A32E7CCB8A98B88345EBDCB0B01E2E1F0D731EECC407CCD648C8388468CD922A73C1C08C48091066E6028B25D58391DCE5E837010959ED7AC9A2176616C4985BBB80E6738EAFCE7BEA3C3E1ABB68C4927BA8C04CCCF6DC8A051168A4A9960496025C9E2C8788ADA03BC1F. How to change setting in web.config

    – Nishan
    Nov 23 '18 at 8:16











  • Make sure that client browser had enabled cookies, also use <authentication mode="Forms">. The User.Identity.Name property retrieve its value from request cookie.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:18



















  • What's your problem then? Is that the username not displayed or something else? Please explain what thing doesn't work, also consider using Membership or Identity provider instead of custom-made authentication.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:07











  • I was unable to get logged in username and also Authorize doesn't work in controller

    – Nishan
    Nov 23 '18 at 8:08











  • Do you have an auth cookie maybe????try checking if an auth cookie is created, if not , change the necessary settings in the web.config

    – mahlatse
    Nov 23 '18 at 8:15











  • @mahlatse - auth cookies is created in the format of E63C9EC8287A32E7CCB8A98B88345EBDCB0B01E2E1F0D731EECC407CCD648C8388468CD922A73C1C08C48091066E6028B25D58391DCE5E837010959ED7AC9A2176616C4985BBB80E6738EAFCE7BEA3C3E1ABB68C4927BA8C04CCCF6DC8A051168A4A9960496025C9E2C8788ADA03BC1F. How to change setting in web.config

    – Nishan
    Nov 23 '18 at 8:16











  • Make sure that client browser had enabled cookies, also use <authentication mode="Forms">. The User.Identity.Name property retrieve its value from request cookie.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:18

















What's your problem then? Is that the username not displayed or something else? Please explain what thing doesn't work, also consider using Membership or Identity provider instead of custom-made authentication.

– Tetsuya Yamamoto
Nov 23 '18 at 8:07





What's your problem then? Is that the username not displayed or something else? Please explain what thing doesn't work, also consider using Membership or Identity provider instead of custom-made authentication.

– Tetsuya Yamamoto
Nov 23 '18 at 8:07













I was unable to get logged in username and also Authorize doesn't work in controller

– Nishan
Nov 23 '18 at 8:08





I was unable to get logged in username and also Authorize doesn't work in controller

– Nishan
Nov 23 '18 at 8:08













Do you have an auth cookie maybe????try checking if an auth cookie is created, if not , change the necessary settings in the web.config

– mahlatse
Nov 23 '18 at 8:15





Do you have an auth cookie maybe????try checking if an auth cookie is created, if not , change the necessary settings in the web.config

– mahlatse
Nov 23 '18 at 8:15













@mahlatse - auth cookies is created in the format of E63C9EC8287A32E7CCB8A98B88345EBDCB0B01E2E1F0D731EECC407CCD648C8388468CD922A73C1C08C48091066E6028B25D58391DCE5E837010959ED7AC9A2176616C4985BBB80E6738EAFCE7BEA3C3E1ABB68C4927BA8C04CCCF6DC8A051168A4A9960496025C9E2C8788ADA03BC1F. How to change setting in web.config

– Nishan
Nov 23 '18 at 8:16





@mahlatse - auth cookies is created in the format of E63C9EC8287A32E7CCB8A98B88345EBDCB0B01E2E1F0D731EECC407CCD648C8388468CD922A73C1C08C48091066E6028B25D58391DCE5E837010959ED7AC9A2176616C4985BBB80E6738EAFCE7BEA3C3E1ABB68C4927BA8C04CCCF6DC8A051168A4A9960496025C9E2C8788ADA03BC1F. How to change setting in web.config

– Nishan
Nov 23 '18 at 8:16













Make sure that client browser had enabled cookies, also use <authentication mode="Forms">. The User.Identity.Name property retrieve its value from request cookie.

– Tetsuya Yamamoto
Nov 23 '18 at 8:18





Make sure that client browser had enabled cookies, also use <authentication mode="Forms">. The User.Identity.Name property retrieve its value from request cookie.

– Tetsuya Yamamoto
Nov 23 '18 at 8:18












1 Answer
1






active

oldest

votes


















1














Did you add



<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>


inside <system.web> section in web.config






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442739%2fasp-net-mvc-custom-authentication-doent-work%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Did you add



    <authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>


    inside <system.web> section in web.config






    share|improve this answer




























      1














      Did you add



      <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
      </authentication>


      inside <system.web> section in web.config






      share|improve this answer


























        1












        1








        1







        Did you add



        <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880" />
        </authentication>


        inside <system.web> section in web.config






        share|improve this answer













        Did you add



        <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880" />
        </authentication>


        inside <system.web> section in web.config







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:25









        HoaxHoax

        419413




        419413
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442739%2fasp-net-mvc-custom-authentication-doent-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin