获取表单验证当前的MembershipUser [英] Get current MembershipUser in Forms Authentication

查看:96
本文介绍了获取表单验证当前的MembershipUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得当前的MembershipUser 从我的网页。

  Membership.GetUser();

返回null。

不能使用

  HttpContext.Current.User.Identity.Name

因为它返回我的配置文件名按我的配置来获得用户名也。

我如何得到我的当前用户?

更新:会员供应商是的SqlMembershipProvider


解决方案

  1. Membership.GetUser()将只用于身份验证的用户使用。否则,它会返回null。要验证你正在处理一个身份验证的请求调用User.Identity.IsAuthenticated页面上。如果你有一个身份验证的请求,但Membership.GetUser()仍然返回null,则意味着与认证的用户关联的用户名不能在会员数据源中找到。验证与User.Identity.Name经过身份验证的用户的用户名。


  2. 如果你调用Membership.GetUser()重载这需要用户名中的一个,它的返回null,则该用户不能在会员数据源存在(或者我们已经有了一个bug)。很容易验证的一种方法是尝试Membership.CreateUser()使用相同的用户名。如果不抛出,因为重复的用户的一个错误,那么你知道用户在根本不存在。


  3. Membership.GetUser()应当为匿名用户从来没有工作过。不支持内置到会员办理此案。


I would like to get the current MembershipUser from my page.

Membership.GetUser();

returns null.

Cannot use

HttpContext.Current.User.Identity.Name

as it returns my profile name as per my configuration to get User Name also.

How do I get my current user?

Update: Membership provider is SqlMembershipProvider

解决方案

  1. Membership.GetUser() will only work for an authenticated user. Otherwise, it's going to return null. To verify you're dealing with an authenticated request call "User.Identity.IsAuthenticated" on the page. If you've got an authenticated request, but Membership.GetUser() is still returning null, then that means the username associated with the authenticated user can't be found in the Membership datasource. Verify the username of the authenticated user with "User.Identity.Name".

  2. If you're calling one of the Membership.GetUser() overloads which takes the username and it's returning null, then that user doesn't exist in the Membership datasource (or we've got a bug). One way to easily verify this is to try a Membership.CreateUser() with the same username. If this doesn't throw an error because of a duplicate user, then you know the user never existed in the first place.

  3. Membership.GetUser() should have never worked for an anonymous user. No support was built into Membership for handling this case.

这篇关于获取表单验证当前的MembershipUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆