分类 C# 下的文章

错误内容:

提示:找不到导入的项目“C:\Program Files\Microsoft Visual
Studio\2022\Community\MSBuild\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets”。此外,已尝试在
$(MSBuildExtensionsPath32) - "C:\Program Files (x86)\MSBuild"
的回退搜索路径中找到“Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets”。这些搜索路径在“C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\17.0_fa40166f\devenv.exe.config”中定义。确认
声明中的路径正确,并且该文件位于某个搜索路径中的磁盘上。

解决办法

修改 .csproj:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" />

将路径中的 v16.0 改成:v$(VisualStudioVersion)

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />

建类:ViewBagActionFilter

namespace MetaWeb.Core
{
    public class ViewBagActionFilter : ActionFilterAttribute
    {

        public ViewBagActionFilter()
        {
            //DI will inject what you need here
        }

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            // for razor pages
            if (context.Controller is PageModel)
            {
                var controller = context.Controller as PageModel;
                controller?.ViewData.Add("Langs", Langs.instance());
                //also you have access to the httpcontext & route in controller.HttpContext & controller.RouteData
            }

            // for Razor Views
            if (context.Controller is Controller)
            {
                var controller = context.Controller as Controller;
                if(controller != null)
                    controller.ViewBag.Langs = Langs.instance();
            }

            base.OnResultExecuting(context);
        }
    }
}

Program.cs 中添加:

builder.Services.AddControllersWithViews(options => {
    options.Filters.Add<ViewBagActionFilter>();
});

新建处理类:

public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
    public string? TransformOutbound(object? value)
    {
        if(value == null) return null;
        string? result = value.ToString();
        if(result == null) return null;

        return Regex.Replace(result, "([a-z])([A-Z])", "$1-$2").ToLower();
    }
}

路由注入 Program.cs :

builder.Services.AddRouting(options => {
    options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
    options.LowercaseUrls = true;
});

路由:

app.MapControllerRoute(name: "default", pattern: "{controller:slugify=Home}/{action:slugify=Index}/{id?}");

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(o =>
    {
        o.LoginPath = "/user/login";
    });

app.UseRouting();
app.UseAuthentication();    //增加登录验证,注意顺序
app.UseAuthorization();

登录验证成功后:

var claims = new List<Claim>()
{
    new Claim(ClaimTypes.Name, userInfo.Nickname),
    new Claim(ClaimTypes.NameIdentifier, userInfo.ID.ToString()),
    new Claim("Phone", userInfo.Phone)
};
var claimnsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

//它会自动发送token给客户端。并生成cookies
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimnsIdentity), 
    new AuthenticationProperties
    {
        IsPersistent = true
    });

验证:

context.HttpContext.User.Identity.IsAuthenticated

取值:

string? Nickname = context.HttpContext.User.Identity.Name
string? uid = context.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
string phone = context.HttpContext.User.FindFirst("Phone")?.Value ?? "18011112222";