ASP.NET MVC2 +会员资格

发布于 2024-11-13 06:21:44 字数 1564 浏览 2 评论 0原文

当新用户刚刚注册到 ASP.NET MVC 应用程序时,我想在我的用户表中创建用户(我使用默认的 ASP.NET MVC 日志记录系统 - 成员资格。我想获取新注册用户的 guid 并在我的应用程序中创建用户数据库将具有相同的 guid 并保存不同的数据,例如姓名等。我该怎么办?我怎样才能获得注册的人 ID?我试图这样做,

public ActionResult Register(RegisterModel model) function in AccountController

但我不知道如何获得正确的数据注册用户的 guid 这不起作用:

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

        if (createStatus == MembershipCreateStatus.Success)
        {
            FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

            //string guid = Helpers.getLoggedUserGuid(); //return null

           //if (Request.IsAuthenticated) { } // return false
           //if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) {} // return false
           //Membership.getUser() // return null
           //The most funny is that after this redirection below the user is logged in - I do not understand it
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
    return View(model);
}

是否有可能在此函数中获取此 guid?(我知道我可以获取用户名等,但我不明白为什么我无法获取此 guid)

I would like to create user in my user table when new user have just registered to ASP.NET MVC application (I use the default ASP.NET MVC logging system - Membership. I want to get the new registered user's guid and create user in my database which will have the same guid and keep there different data like name surname etc. How can I do it? How can I just get the registered person ID? I was trying to do it in

public ActionResult Register(RegisterModel model) function in AccountController

but I do not know how to get the just registered user's guid this is not working:

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

        if (createStatus == MembershipCreateStatus.Success)
        {
            FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

            //string guid = Helpers.getLoggedUserGuid(); //return null

           //if (Request.IsAuthenticated) { } // return false
           //if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) {} // return false
           //Membership.getUser() // return null
           //The most funny is that after this redirection below the user is logged in - I do not understand it
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
    return View(model);
}

Is any possibility to get this guid in this function? (I know that I can get userName and so on but I can not understand why I can not get this guid)

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

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

发布评论

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

评论(2

反目相谮 2024-11-20 06:21:44

您可以从 MembershipUser 获取 Guid:

var user = Membership.GetUser(model.Username);
var guid = (Guid) user.ProviderUserKey;

MSDN:

ProviderUserKey 属性公开
会员身份的标识符
数据源类型为对象。类型
标识符的值取决于
会员提供者或
会员用户。在这种情况下
SqlMembershipProvider,
ProviderUserKey 可以转换为 Guid,
由于 SqlMembershipProvider 存储
用户标识符作为
唯一标识符。

You can get Guid from MembershipUser:

var user = Membership.GetUser(model.Username);
var guid = (Guid) user.ProviderUserKey;

MSDN:

The ProviderUserKey property exposes
the identifier from the membership
data source typed as object. The type
of the identifier depends on the
MembershipProvider or the
MembershipUser. In the case of the
SqlMembershipProvider, the
ProviderUserKey can be cast as a Guid,
since the SqlMembershipProvider stores
the user identifier as a
UniqueIdentifier.

迷乱花海 2024-11-20 06:21:44

您无法通过您尝试的方法获得该值。除非您使用 Membership.GetUser()

应该做的是:

创建一个通用控制器,例如:

public class MainController : Controller
{
    public MembershipUser _user = null;
    public MembershipUser User {
        if(_user == null) _user = Membership.GetUser(model.Username);
        return _user;
    }
    public GUID UserGuid {
        get { return (Guid)User.ProviderUserKey;; }
    }
}

然后,而不是让您的控制器派生自 Controller 使它们继承自新的自定义控制器

public class SalesController : MainController

,并且在您需要的任何方法中,您只需调用 this.User 来保存 MembershipUserthis.User 。 UserGuid 获取用户 ID。

You can't get that value in the method you are trying. unless you use the Membership.GetUser()

What you should do is:

create a common controller, for example:

public class MainController : Controller
{
    public MembershipUser _user = null;
    public MembershipUser User {
        if(_user == null) _user = Membership.GetUser(model.Username);
        return _user;
    }
    public GUID UserGuid {
        get { return (Guid)User.ProviderUserKey;; }
    }
}

and then, instead of having your Controllers derivate from Controller make them inherit from your new custom Controller

public class SalesController : MainController

and in any method you need, you just call this.User to hold the MembershipUser or this.UserGuid to get the User Id.

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