c#身份登录api控制器中的用户
我正在尝试在web api控制器中获取登录用户。当我这么做的时候c#身份登录api控制器中的用户,c#,asp.net-web-api,C#,Asp.net Web Api,我正在尝试在web api控制器中获取登录用户。当我这么做的时候 User.Identity.GetUserId(); 它返回null 但我的用户已登录。有人知道问题是什么吗 这些是我在API类中使用的 using AltuaWebsite.Core.Models; using System; using System.Web; using System.Web.Http; 这是用户登录系统的方式 public ActionResult Login(String email, String
User.Identity.GetUserId();
它返回null
但我的用户已登录。有人知道问题是什么吗
这些是我在API类中使用的
using AltuaWebsite.Core.Models;
using System;
using System.Web;
using System.Web.Http;
这是用户登录系统的方式
public ActionResult Login(String email, String password)
{
ApplicationUser user = UserManager.FindByName(email);
if (user != null && checkRole("admin,user", user.Id) && UserManager.CheckPassword(user, password))
{
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, email)
},
DefaultAuthenticationTypes.ApplicationCookie,
ClaimTypes.Name, ClaimTypes.Role);
foreach (var role in user.Roles)
{
identity.AddClaim(new Claim(ClaimTypes.Role, role.Name));
}
Authentication.SignIn(new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddHours(24)
}, identity);
return RedirectToAction("Index", "Dashboard");
}
else
{
return RedirectToAction("Index", "Site");
}
}
您也可以直接在控制器中尝试以下操作
var claimsPrincipal = (ClaimsPrincipal)ActionContext.RequestContext.Principal;
您是否使用System.Web.Http添加;他们是如何登录的?您的应用程序中是否有登录表单?NT身份验证,基本身份验证?请阅读并展示您尝试过的内容。首先解释您正在使用的身份验证。User.Identity.GetUserId()的null值在哪里;在另一个api控制器中。