简体   繁体   English

使用表单身份验证在ASP.NET Webforms App外部的类库中检索当前登录的用户ID

[英]Retrieving the current logged in user's Id in a class library outside of an ASP.NET Webforms App using forms authentication

Currently in every one of my model classes I am doing the following in the constructor. 当前,在我的每个模型类中,我都在构造函数中执行以下操作。

    public Product()
    {
        CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;
        ModifiedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;
    }

This way when saving/updating records these values will always be set. 这样,在保存/更新记录时,将始终设置这些值。

However, I want to switch my application from using the logged in user's name which the above gets me, to using the logged in user's Id. 但是,我想将我的应用程序从上面获取的登录用户名切换为使用登录用户ID。

With forms authentication what is the best way to retrieve additional data about the current logged in user without using Session? 使用表单身份验证,什么是不使用Session来检索有关当前登录用户的其他数据的最佳方法是什么? I noticed you can pass additional data when creating the forms auth cookie as soon as the user logs in, but I would have no idea how to retrieve it. 我注意到您可以在用户登录后立即创建表单身份验证cookie时传递其他数据,但是我不知道如何检索它。

Here is the solution I came up with. 这是我想出的解决方案。 I would appreciate any comments or suggestions. 我将不胜感激任何意见或建议。 It feels kind of strange that everytime I create one of these model objects on the UI I need to pass the userId in the constructor: 每当我在UI上创建这些模型对象之一时,我都需要在构造函数中传递userId,这感觉有点奇怪:

public abstract class AuditableEntity : BaseEntity
{
    public DateTime CreatedDate { get; set; }
    public int? CreatedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public int? ModifiedBy { get; set; }
}

public class Product : AuditableEntity
{
    public User(int userId)
    {
        this.CreatedBy = userId;
        this.ModifiedBy = userId;
    }

    public string ProductName { get; set; }
    public string SerialNumber { get; set; }
}

When authenticating an user, userYou can use FormsAuthenticationTicket to pass all cookie information and additional userData (string) to it, here is an example , then retrieve it by: 验证用户身份时,用户可以使用FormsAuthenticationTicket向其传递所有cookie信息和其他userData(字符串), 这是一个示例 ,然后通过以下方式进行检索:

       FormsIdentity id = (FormsIdentity)User.Identity;
       FormsAuthenticationTicket ticket = id.Ticket;

       //those are label ids, that why they have Text property 
       cookiePath.Text = ticket.CookiePath;
       expireDate.Text = ticket.Expiration.ToString();
       expired.Text = ticket.Expired.ToString();
       isPersistent.Text = ticket.IsPersistent.ToString();
       issueDate.Text = ticket.IssueDate.ToString();
       name.Text = ticket.Name;
       userData.Text = ticket.UserData; // string userData you passed is HERE
       version.Text = ticket.Version.ToString();

More information 更多信息

By the way, you should use HttpContext.Current.User.Identity.Name , take a look at this and this maybe this question . 顺便说一句,你应该使用HttpContext.Current.User.Identity.Name ,看看这个这个 ,也许这个问题

You mentioned you have a lot of classes that have these CreatedBy and ModifiedBy properties. 您提到了很多具有这些CreatedBy和ModifiedBy属性的类。

Have them implement an interface: 让他们实现一个接口:

interface IAuditableEntity
{
    int CreatedBy {get; set;}
    int ModifiedBy {get; set;}
}

public class Product : IAuditableEntity
{
    public string ProductName { get; set; }
    public string SerialNumber { get; set; }
    public int CreatedBy {get; set;}
    public int ModifiedBy {get; set;}
}
public class Vendor : IAuditableEntity
{
    public string VendorName{ get; set; }
    public string VendorNumber { get; set; }
    public int CreatedBy {get; set;}
    public int ModifiedBy {get; set;}
}

Create a centralized location in your web application responsible for communicating to the BLL that sets these properties for you. 在Web应用程序中创建一个集中位置,该位置负责与BLL进行通信,该BLL为您设置了这些属性。

public class BllLink
{
    public static void SaveEntity(IAuditableEntity entity)
    {
        entity.ModifiedBy = HttpContext.Current.User.Identity.Name;
        if(String.IsNullOrEmpty(entity.CreatedBy)) //assume new
        {
            entity.CreatedBy = HttpContext.Current.User.Identity.Name;
        }
        Bll.SaveEntity(entity); //you might need to use generics
    }
}

When you see repetitive code, it's a good candidate for simplification. 当您看到重复的代码时,它是简化的理想选择。 Learning how to effectively utilize generics and interfaces will go a long ways towards that. 学习如何有效利用泛型和接口将大大有助于这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取当前登录用户的ID ASP.NET Web Forms - Get ID of current logged in user in ASP.NET Web Forms ASP.NET中的类库中的Webforms - Webforms in class library in asp.net Asp.net MVC表单身份验证防止创建时自动记录用户 - Asp.net mvc Forms Authentication Prevent user from been automatically logged when created 检测使用ASP.NET应用程序登录计算机的用户 - Detect user logged on a computer using ASP.NET app 运行ASP.NET WebForms应用程序时从类库的代码中读取安全的.pem文件 - Reading a secure .pem file from a class library's code when running ASP.NET WebForms application 用户模拟asp.net表单身份验证 - User impersonation with asp.net forms authentication Asp.net 4.8 WebForms授权使用Owin OpenId Connect Authentication (app.UseOpenIdConnectAuthentication) - Asp.net 4.8 WebForms authorization using Owin OpenId Connect Authentication (app.UseOpenIdConnectAuthentication) 使用窗体身份验证登录时使用WindowsIdentity获取ASP.NET中的文件/目录列表 - Using WindowsIdentity to get a list of files/directories in ASP.NET while logged in using Forms Authentication 在ASP.NET WebForms中将OAuth与令牌一起使用Azure身份验证不是MVC - Azure Authentication using OAuth with token in ASP.NET WebForms NOT MVC 使用LINQ从ASP.net中的Webforms调用类库项目中编写的方法到实体 - Calling methods written in class library project from webforms in ASP.net using LINQ to entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM