相关疑难解决方法(0)

找不到Request.GetOwinContext

我一直在寻找一个小时试图弄清楚为什么这不起作用.

我有一个带有WebAPI的ASP.Net MVC 5应用程序.我正在尝试获取Request.GetOwinContext().身份验证,但我似乎无法找到如何包含GetOwinContext.这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TaskPro.Models;

namespace TaskPro.Controllers.api
{
    public class AccountController : ApiController
    {
        [HttpPost]
        [AllowAnonymous]
        public ReturnStatus Login(LoginViewModel model)
        { 
            if (ModelState.IsValid)
            {
                var ctx = Request.GetOwinContext(); // <-- Can't find this

                return ReturnStatus.ReturnStatusSuccess();
            }

            return base.ReturnStatusErrorsFromModelState(ModelState);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从我读过的内容来看,它应该是System.Net.Http的一部分,但我已经包含了它,但它仍然没有解决.Ctrl-Space也没有给我任何intellisense选项.

我在这里错过了什么?

c# asp.net-web-api owin

93
推荐指数
6
解决办法
8万
查看次数

访问AccountController外部的UserManager

我试图aspnetuser从不同的控制器(而不是accountcontroller)设置表中的列的值.我一直试图访问,UserManager但我不知道如何做到这一点.

到目前为止,我已经在控制器中尝试了以下内容我想用它:

    ApplicationUser u = UserManager.FindById(User.Identity.GetUserId());
    u.IsRegComplete = true;
    UserManager.Update(u);
Run Code Online (Sandbox Code Playgroud)

这不会编译(我想因为UserManager还没有实例化控制器)

我还尝试创建一个公共方法,AccountController以接受我想要更改值的值并在那里做,但我无法弄清楚如何调用它.

public void setIsRegComplete(Boolean setValue)
{
    ApplicationUser u = UserManager.FindById(User.Identity.GetUserId());
    u.IsRegComplete = setValue;
    UserManager.Update(u);

    return;
}
Run Code Online (Sandbox Code Playgroud)

如何访问和编辑帐户控制器之外的用户数据?

更新:

我尝试在其他控制器中实例化UserManager,如下所示:

    var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    ApplicationUser u = userManager.FindById(User.Identity.GetUserId());
Run Code Online (Sandbox Code Playgroud)

我的项目符合(有点兴奋),但当我运行代码时,我收到以下错误:

Additional information: The entity type ApplicationUser is not part of the model for the current context.
Run Code Online (Sandbox Code Playgroud)

更新2:

我已将该功能移至IdentityModel(不要问我在这里抓住吸管),如下所示:

   public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc actioncontroller asp.net-mvc-5 asp.net-identity-2

28
推荐指数
2
解决办法
3万
查看次数