如何从UserProfile获取自定义信息? [英] How to get custom information from UserProfile?

查看:125
本文介绍了如何从UserProfile获取自定义信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UserProfile 中添加自定义字段 UserType 作为字符串.我想更改我的login post method to get the value from UserType并将此值传递给TempData.

I add in my UserProfile the custom field UserType as string. I want to change my login post method to get the value from UserType and pass this value in a TempData.

这是我在AccountController上的代码:

This is my code on AccountController:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
        {
            var context = new UsersContext();
            var currentUser = Membership.GetUser(User.Identity.Name);
            string username = currentUser.UserName;
            var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
            var userType = user.UserType;

            TempData["UserType"] = user.UserName; //Taking UserType from User Profile to validate in _Layout
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model); 
    }

我对此方法进行调试,这是我所拥有的信息:

I do a debug on this method, this is the informations that i have:

> **On WebSecurity.Login()** I can see the UserName and Password.
> **Var CurrentUser**  I can't get the value from User.Identity.Name
> **string Username** This error is displayed *Object reference not set to an instance of an object*. Application crash.

有人对此有解决方案吗? 我是ASP.NET的新手,但也许如果我实现自定义成员身份,它将起作用,您怎么看?你能给我举个例子吗?谢谢你们.

Anybody have some solution for this? I'm new on ASP.NET, but maybe if I implements custom membership it will work, what do you think? Could you send me some example? Thank you guys.

解决方案在这里:

`[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] 公共ActionResult登录(LoginModel模型,字符串returnUrl) { 如果(ModelState.IsValid&& WebSecurity.Login(model.UserName,model.Password,persistCookie:model.RememberMe)) { var context = new UsersContext(); var user = context.UserProfiles.Where(u => u.UserName == model.UserName); var userType = user.Select(m => m.UserType);

`[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { var context = new UsersContext(); var user = context.UserProfiles.Where(u => u.UserName == model.UserName); var userType = user.Select(m => m.UserType);

        TempData["UserType"] = user.UserName; //Taking UserType from User Profile to validate in _Layout
        return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model); 
}`

推荐答案

只是一个主意,以防万一我无法评论我的声誉, 为什么不直接使用model.UserName而不是var currentUser = Membership.GetUser(User.Identity.Name);

Just an Idea for in case I cannot Comment bcoz of my reputation, why are you not directly using model.UserName instead of using var currentUser = Membership.GetUser(User.Identity.Name);

造成这种情况的原因是,任何登录的人都是其自身的当前用户,您可以从数据库中检索其详细信息.

The Reason behind this is any one who logins will be the current user it self and you can retrieve its details form the database.

已更新

您可以使用

UserProfile User = udb.UserProfiles.Where(m => m.UserName == model.UserName).FirstOrDefault();
var userType=User.UserType; // this brings your UserType from database

// then you can use this as
TempData["UserType"]=userType;

希望这对您有帮助!

这篇关于如何从UserProfile获取自定义信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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