开发者

ASP.NET MVC2 + Membership

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
        Member开发者_开发知识库shipCreateStatus 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)


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜