自定义membershipprovider验证成功但无法GetUser()

发布于 2024-11-29 10:02:55 字数 1162 浏览 0 评论 0原文

大家好 Stackoverflow 用户,

我目前正在深入研究 WCF,并且目前已经设置了一个使用 clientCredentialType="UserName" 的 POC Web 服务。由于我们有一些独特的愿望,我们选择了定制的会员提供商。我目前正在一点一点地实施。这意味着大多数方法(尚未使用)都会抛出未实现的异常。

实现的方法是 Initialize() 和 ValidateUser() 方法,验证用户工作,当给定正确的凭据时,一个人被验证,当我输入 bogus 时,他们会得到一个安全异常。

到目前为止,一切都很好。

然而,当我调用 GetUser() 方法(从某处获取用户名然后调用)时:

public override MembershipUser GetUser(string username, bool userIsOnline)
{
    throw new NotImplementedException();
}

如您所见,这没有实现(很明显,它不起作用),目前令我沮丧的是我无法让它工作,因为从底层 GetUser() 方法(从基类 MembershipProvider)传递的用户名传递了一个空字符串。

有什么具体的事情我忘记了吗?还有其他人有解决此问题的自定义会员资格提供商的经验吗?

编辑:添加了调用堆栈

at System.Web.Security.Membership.GetUser(字符串用户名,布尔值 userIsOnline) 在 System.Web.Security.Membership.GetUser() 在 D:\Projects\IBS 中的 DirectPay.WCF.Services.Service.TestConnection() 项目\DirectPay\source\WCF.Services\Service.svc.cs:第 21 行 SyncInvokeTestConnection(对象,对象[],对象[])在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象 实例,对象[]输入,对象[]&输出)在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& 远程过程调用)

Hello fellow Stackoverflow users,

I'm currently diving into WCF a bit and i've currently setup a POC webservice which uses clientCredentialType="UserName". Since we some unique wishes we opted for a custom membershipprovider. I am currently implementing one piece by piece. This means that most methods (which aren't used yet) throw not implemented exceptions.

The methods implemented are the Initialize() and ValidateUser() methods, the validate user works, when given the right credentials a person is validate, when i enter bogus they get a security exception.

So far so good.

How ever when i call the GetUser() method (which fetched the username from somewhere and then calls):

public override MembershipUser GetUser(string username, bool userIsOnline)
{
    throw new NotImplementedException();
}

As you can see, this is not Implemented (so obviously, it does not work), what frustrates me at the moment is that i can't get this to work because the username which is passed from the underlaying GetUser() method (from the base class MembershipProvider) passes an empty string.

Is there something specific which i'm forgetting? Has anyone else got experience with a custom membershipprovider which tackled this issue?

Edit: added the callstack

at System.Web.Security.Membership.GetUser(String username, Boolean
userIsOnline) at System.Web.Security.Membership.GetUser() at
DirectPay.WCF.Services.Service.TestConnection() in D:\Projects\IBS
projects\DirectPay\source\WCF.Services\Service.svc.cs:line 21 at
SyncInvokeTestConnection(Object , Object[] , Object[] ) at
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object
instance, Object[] inputs, Object[]& outputs) at
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
rpc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云不回头 2024-12-06 10:02:55

用户通常应该存储在您自己的自定义数据库中,这是实现自定义会员资格提供程序的要点。这是我对同一功能的实现:

/// <summary>
/// Gets information from the data source for a user.
/// Provides an option to update the last-activity date/time stamp for the user.
/// </summary>
/// <param name="username">The name of the user to get information for.</param>
/// <param name="userIsOnline">true to update the last-activity date/time 
/// stamp for the user; false to return user information without updating 
/// the last-activity date/time stamp for the user.</param>
/// <returns>A MembershipUser object populated with the specified
/// user's information from the data source.</returns>
public override MembershipUser GetUser(string username, bool userIsOnline)
{
    // Get User from my custom database
    User user = Database.User.GetUser(username);

    // Convert User to MebmershipUser (or return null if User == null)
    return user == null ? null : new MembershipUser(
        "CustomMembershipProvider",
        user.UserName, 
        user.UserID,
        user.Email,
        string.Empty, 
        user.Comments, 
        user.Active,
        false, 
        user.CreatedDate, 
        user.LastLoginDate, 
        DateTime.Now, 
        user.LastPasswordChangedDate, 
        DateTime.Now);
}

The user is generally supposed to be stored in a custom database of your own which is the point of implementing a custom membership provider. Here is my implementation of the same function:

/// <summary>
/// Gets information from the data source for a user.
/// Provides an option to update the last-activity date/time stamp for the user.
/// </summary>
/// <param name="username">The name of the user to get information for.</param>
/// <param name="userIsOnline">true to update the last-activity date/time 
/// stamp for the user; false to return user information without updating 
/// the last-activity date/time stamp for the user.</param>
/// <returns>A MembershipUser object populated with the specified
/// user's information from the data source.</returns>
public override MembershipUser GetUser(string username, bool userIsOnline)
{
    // Get User from my custom database
    User user = Database.User.GetUser(username);

    // Convert User to MebmershipUser (or return null if User == null)
    return user == null ? null : new MembershipUser(
        "CustomMembershipProvider",
        user.UserName, 
        user.UserID,
        user.Email,
        string.Empty, 
        user.Comments, 
        user.Active,
        false, 
        user.CreatedDate, 
        user.LastLoginDate, 
        DateTime.Now, 
        user.LastPasswordChangedDate, 
        DateTime.Now);
}
深巷少女 2024-12-06 10:02:55

好吧,在我把头撞到墙上几个小时后(很可能是因为我还不知道 Jack 关于 WCF ^^)之后,我当前的答案是发现你可以通过以下方式获取当前用户:

ServiceSecurityContext.Current.PrimaryIdentity.Name;

遗憾的是,GetUser() 方法仍然可以这不是我想要的,解决方法是在我的自定义会员资格提供程序中覆盖此方法并在那里获取名称。另一种选择是将以下内容添加到 GetUser(string username, bool userIsOnline) 方法中:

if(string.IsNullOrWhiteSpace(username))
{
   username = ServiceSecurityContext.Current.PrimaryIdentity.Name;
}

我很想找到一种无需手动获取用户名的方法,所以我不会将此标记为您的答案,如果任何事情都是一个解决方法。

Well, my current answer after beating my head against a wall for a few hours (most likely because i dont know jack about WCF yet ^^) is finding out you can get the current user with:

ServiceSecurityContext.Current.PrimaryIdentity.Name;

The GetUser() method sadly, still does not what i would like, a work around would be to override this method in my custom membershipprovider and fetch the name there. Another option would be to add the following to the GetUser(string username, bool userIsOnline) method:

if(string.IsNullOrWhiteSpace(username))
{
   username = ServiceSecurityContext.Current.PrimaryIdentity.Name;
}

I would love to find a way without having to fetch the Username manually, so i won't mark this as an answer as of your, if anything it's a workaround.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文