3

I've implemented my own AccountProfile class is ASP.net MVC and it works but now I'm running into a strange problem. First off, I'm calling AccountProfile.MyProperty in the Views/Shared/_LoginPartial.cshtml. AccountProfile.MyProperty makes a call to Membership.GetUser().UserName to work. Now, when I "signup" for an account, after I'm logged in, AccountProfile.MyProperty works and renders info into the html page.

However when I stop running, change some code, and launch again, Membership.GetUser() returns null, even though I'm still logged in according to membership.

After the page fails to load, if I navigate to the /Account/Login page, Membership.GetUser() works. After it works, I can then navigate to the index page that didn't work at start, and it works.

My question is why does Membership.GetUser() return null when my page first loads up?

Thanks.

Community
  • 1
  • 1
Clay Smith
  • 1,051
  • 14
  • 27
  • 2
    Sounds like you are requesting Memberhip.GetUser() before authentication kicks in. – Grimace of Despair Sep 25 '12 at 02:22
  • How can I work around that? If I start at or goto Account/Login page, GetUser works from there on out (and I don't even log in). However, if I try to start on another page, it doesn't work :( – Clay Smith Sep 26 '12 at 03:08
  • Also User.Identity.IsAuthenticated is returning true when Membership.GetUser() is returning null. – Clay Smith Sep 26 '12 at 03:11

5 Answers5

2

Alright, I found a work around the buggy Membership.GetUser() function.

HttpContext.Current.User.Identity.Name

This piece of code gets the logged in users name, and it actually appears to work. If I have any problems with it I'll report back here.

Clay Smith
  • 1,051
  • 14
  • 27
  • Really? Why would someone 'down vote' the solution to my problem? Membership.GetUser() is clearly returning null when the user is logged in and authenticated. Additionally, it only returns null on the first time the user tries to load the home page, but not when they try to load the /Account/Login page. After they load /Account/Login page, they can then load the home page fine. HttpContext.Current.User.Identity.Name works, and Membership.GetUser() doesn't work when the user is already logged in and authenticated. This is on MVC 4 .NET v4.5. – Clay Smith Sep 26 '12 at 12:04
  • It's downvoted because it is not a solution. You have an other problem in your code causing this behavour. Your solution is a workaround, not a fix. – Peter Aug 30 '13 at 09:03
1

Please take a look at the following posting, perhaps you having the same issue? :

Hope this helps

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43
  • does not fix the problem :( – Clay Smith Sep 26 '12 at 02:53
  • Try another link. There can be 1001 reasons for this, just try one by one, hopefully you fall into the mainstream problem category :) Try, please let me know if it works for you and if article was of help. – Display Name Sep 26 '12 at 12:58
1

Try this

using Microsoft.AspNet.Identity;

var id = User.Identity.GetUserId();
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
0

Just came across same issue. In MVC 4, default authentication system is initialized using filter InitializeSimpleMembershipAttribute (it's pretty clear from source code of generated project). And filter is applied only to AccountController (so, once you visit login or any other page of this controller, authentication system is initialized and you can use Membership.GetUser() normally wherever you need it).

For my current project, quick and dirty solution is to have some controller, defined like this:

[InitializeSimpleMembership]
public abstract class DefaultController : Controller
{}

And deriving other controllers of my application from DefaultController, not from Controller directly. Also, this technique is nice for having commonly used properties (like dbcontext variables etc.), at one place.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Dmytro
  • 1,590
  • 14
  • 14
0

Add [InitializeSimpleMembership] to the Controller

Example:

using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using Bencsharp.BL.Services;
using Bencsharp.Mvc.Filters;
using Bencsharp.Mvc.Models;

namespace Bencsharp.Mvc.Controllers
{
    [InitializeSimpleMembership]
    public class HomeController : Controller
    {
        // ...
    }
}