开发者

FormsAuthentication problem

开发者 https://www.devze.com 2023-03-21 03:18 出处:网络
I \'form authenticate\' users like this in the controller: FormsAuthentication.SetAuthCookie(User.Email, false);

I 'form authenticate' users like this in the controller:

FormsAuthentication.SetAuthCookie(User.Email, false);

(provided that the credential are valid of course).

Usually, I can access the username of the user like this in a view (I just started to use razor):

@Html.Encode(Page.User.Identity.Name)

Unfortunately, I get:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

I presume that:

 FormsAuthentication.SetAuthCookie(User.Email, 开发者_如何学JAVAfalse);

did not work correctly (login and consequent redirect work fine though).

Can anyone see something wrong? Thanks!

Christian


Don't use Page. You have access to User from within the view on its own.

@Html.Encode(User.Identity.Name)


You can use HttpContext.Current.User.Identity.Name. I would suggest passing the username in as part of your ViewModel though instead of escaping into Asp.Net framework territory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号