问题
I am getting the username from the URL so blah.com/kevinuk.
I want some content on the page to say KevinUK which is whats stored in the membership table but when I do the following, it returns the same casing as what the input was.
MembershipUser member = Membership.GetUser(user);
string userName = member.UserName;
How do I use a lowercase username as the parameter and return the value from the database with the correct casing?
回答1:
It's not clear which Membership provider that you are using, but you can easily descend from that and override the GetUser method.
Create a class that class inherited from MembershipProvider class.
public class MyMembershipProvider : MembershipProvider
{
public MyMembershipProvider()
{
//
// TODO: Add constructor logic here
//
}
}
Override the getUser Method.
public override MembershipUser GetUser(string username, bool userIsOnline)
{
... Logic here to do a case insensitive lookup...
}
Finally update the web config to use your new provider:
<system.web>
<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="10">
<providers>
<add name="MyMembershipProvider" type="Providers.FIFAMembershipProvider" connectionStringName="ADConnectionString" ... />
</providers>
</membership>
</system.web>
Some examples:
http://www.eggheadcafe.com/tutorials/aspnet/30c3a27d-89ff-4f87-9762-37431805ef81/aspnet-custom-membership.aspx
http://msdn.microsoft.com/en-us/library/ms366730(VS.80).aspx
回答2:
Unfortunately, the Membership.GetUser(string username)
method simply sets the MembershipUser
's username to the value of the passed parameter. To get the correct casing, you'll either need to use Membership.GetUser(object providerUserKey)
, which takes the user's GUID, or override the former method and its respective stored procedure to return the properly-cased username.
Or, you could simply make back-to-back calls to the two different GetUser()
methods, but that's quite wasteful.
回答3:
Stumpled on the same issue and tried above solution = Easiest way of getting correct casing:
var user = Membership.GetUser(userName.Text);
user = Membership.GetUser(user.ProviderUserKey);
var correctCasing = user.UserName;
来源:https://stackoverflow.com/questions/1001527/membership-getuserusername-how-to-return-correct-casing