使用ASP成员资格从LDAP检索用户

本文关键字:LDAP 检索 用户 ASP 成员 使用 | 更新日期: 2023-09-27 18:16:23

我的公司正在使用LDAP对其用户进行身份验证。我有登录部分工作,但现在我需要一种方式,一旦登录,我们可以参考用户。我尝试了多种方法,但没有一种是有效的。

下面是我用来登录的代码:

var x = Membership.Provider;
if (Membership.ValidateUser(model.UserName, model.Password))
{
    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/''"))
    {
        return Redirect(returnUrl);
    }
    else
    {
        string userId = Membership.GetUser().ProviderUserKey.ToString();
        return RedirectToAction("Index", "Home");
    }
}

这一行:

string userId = Membership.GetUser().ProviderUserKey.ToString();

我得到一个错误:

参数username不能为空。

我也从这里尝试了一些选项:

如何在ASP中访问UserId ?. NET成员而不使用Membership. getuser ()?

像这样:

MembershipUser CurrentUser = Membership.GetUser(User.Identity.Name);

得到相同的错误

对于任何想知道的人,iamtooamazing建议如下:

string userId = Membership.GetUser(model.UserName).ProviderUserKey.ToString();

在这个方法中可以工作。但是,我不能在解决方案的其他地方使用它。我需要的东西,将工作在所有的控制器(和方法在他们)

使用此代码:

string user = ((CustomIdentity)User.Identity).UserId;

我在那行收到这个错误:

无法强制转换类型的对象"System.Security.Principal。GenericIdentity'类型"STCAuthentication.Models.CustomIdentity"。

这是我代码的当前状态:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        var x = Membership.Provider;
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/''"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                string user = ((CustomIdentity)User.Identity).UserId;
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }
    // If we got this far, something failed, redisplay form
    return View(model);
}

使用ASP成员资格从LDAP检索用户

在这个方法中可以工作。然而,我将无法使用它在溶液中的任何位置。我需要一些能起作用的东西所有的控制器(和其中的方法)

如果你创建一个自定义的标识来存储你的userid呢?

public class CustomIdentity : IIdentity
{
    private IIdentity Identity { get; set; }
    public string UserId { get; private set; }
    public string AuthenticationType { get { return Identity.AuthenticationType; } }
    public bool IsAuthenticated { get { return Identity.IsAuthenticated; } }
    public string Name { get { return Identity.Name; } }
    public CustomIdentity(IIdentity identity)
    {
        Identity = identity;
        UserId = Membership.GetUser(identity.Name).ProviderUserKey.ToString();
    }
}

创建一个CustomPrincipal,

public class CustomPrincipal : IPrincipal
{
    private IPrincipal Principal;
    public IIdentity Identity { get; private set; }
    public bool IsInRole(string role) { return Principal.IsInRole(role); }
    public CustomPrincipal(IPrincipal principal)
    {
        Principal = principal;
        //this force the load of the userid
        Identity = new CustomIdentity(principal.Identity);
    }
}

然后在全局。

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
        if (Request.IsAuthenticated)
        {
            var principal = new CustomPrincipal(HttpContext.Current.User);
            HttpContext.Current.User = principal;
        }
}

你可以像这样访问UserId

((CustomIdentity)User.Identity).UserId //In Controllers
((CustomIdentity)HttpContext.Current.User.Identity).UserId //Inside classes

这是你要找的吗?