اشتراکها
نظرات اشتراکها
توسعه Asp.net Core و Asp.net Core Identity
در ابتدا از توجه شما سپاسگزارم.
تغییرات نسبت به سفارش سازی Identity :
1- AppDbContext
2- ApplicationUser
3- Startup
بنده مشکلی که با سفارش سازی کامل و مخصوصا Identity دارم خطایی در بخش IoC همین پروژه نسبت به IUserStore هست :
و زمانی که این خط از کد را به شکل زیر تغییر میدهم و از کلاس پیشفرض Identity استفاده میکنم خطای بالا برطرف میشود دقیقا مشکل از کجاست هنوز درک نکردم!
عملا با جایگزین کردن خط بالا، این مورد غیر فعال خواهد شد و در این خط با خطای زیر مواجه خواهیم شد:
تغییرات نسبت به سفارش سازی Identity :
1- AppDbContext
2- ApplicationUser
3- Startup
بنده مشکلی که با سفارش سازی کامل و مخصوصا Identity دارم خطایی در بخش IoC همین پروژه نسبت به IUserStore هست :
ErrorCS0311The type 'SampleFive.DomainLayer.Models.ApplicationRole' cannot be used as type parameter 'TRole' in the generic type or method 'UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken>'. There is no implicit reference conversion from 'SampleFive.DomainLayer.Models.ApplicationRole' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole<int, SampleFive.DomainLayer.Models.ApplicationUserRole, Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<int>>'. SampleFive.IoC..NETCoreApp,Version=v1.1 H:\WebApp\CustomizeAspdotnetCore\src\SampleFive.IoC\AppIocConfig.cs32Active
و زمانی که این خط از کد را به شکل زیر تغییر میدهم و از کلاس پیشفرض Identity استفاده میکنم خطای بالا برطرف میشود دقیقا مشکل از کجاست هنوز درک نکردم!
public class ApplicationRole : IdentityRole<int, ApplicationUserRole, IdentityRoleClaim<int>>
عملا با جایگزین کردن خط بالا، این مورد غیر فعال خواهد شد و در این خط با خطای زیر مواجه خواهیم شد:
ErrorCS0311The type 'SampleFive.DomainLayer.Models.ApplicationRole' cannot be used as type parameter 'TRole' in the generic type or method 'IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>'. There is no implicit reference conversion from 'SampleFive.DomainLayer.Models.ApplicationRole' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole<int, SampleFive.DomainLayer.Models.ApplicationUserRole, SampleFive.DomainLayer.Models.ApplicationRoleClaim>'. SampleFive.DataLayer..NETCoreApp,Version=v1.1 H:\WebApp\CustomizeAspdotnetCore\SampleFive.DataLayer\Context\ApplicationDbContext.cs16Active
- Validate arguments from events.
- Validate inputs and results from JS interop calls.
- Avoid using (or validate beforehand) user input for .NET to JS interop calls.
- Prevent the client from allocating an unbound amount of memory.
- Data within the component.
-
DotNetObject
references returned to the client. - Guard against multiple dispatches.
- Cancel long-running operations when the component is disposed.
- Avoid events that produce large amounts of data.
- Avoid using user input as part of calls to NavigationManager.NavigateTo and validate user input for URLs against a set of allowed origins first if unavoidable.
- Don't make authorization decisions based on the state of the UI but only from component state.
- Consider using Content Security Policy (CSP) to protect against XSS attacks.
- Consider using CSP and X-Frame-Options to protect against click-jacking.
- Ensure CORS settings are appropriate when enabling CORS or explicitly disable CORS for Blazor apps.
- Test to ensure that the server-side limits for the Blazor app provide an acceptable user experience without unacceptable levels of risk.
به صورت معمول در سیستمهای مبتنی بر WCF ارتباط بین سرور و کلاینت در قالب EndpointConfiguration تعریف میشوند. یعنی کلاینت برای برقراری ارتباط با سرور نیاز به آدرسی که سرور مورد نظر در آن هاست شده است دارد. این روش هنگامی که فقط یک مقصد وجود داشته باشد روش موثری است. اما اگر سرویسهای مورد نظر در چند سرور هاست شده باشند نیاز به سیستم مسیر یابی خواهیم داشت. خوشبختانه در WCF 4.0 این امکان به خوبی تدارک دیده شده است.
WCF Routing Service چیست؟
Routing Service به عنوان سرویس مسیریابی WCF در دات نت 4 معرفی شد. به وسیله Routing Service میتوان Endpoint Configuration مقصدهای مختلف را با هم تجمیع کرد و در نتیجه تعداد تنظیمات برای Endpoint در سمت کلاینت کاهش پیدا میکند به طوری که کلاینت فقط با یک مقصد در ارتباط است. مقصد کلاینت همان Routing Service میباشد که در این سرور درخواستهای رسیده از کلاینتها با توجه به فیلتر انجام شده به مقصد اصلی ارسال خواهند شد.
با استفاده از Routing Service معماری سیستم به صورت تغییر پیدا میکند:
پیاده سازی Contractهای بالا در فالب سرویس به صورت زیر خواهد بود:
تنظیمات Binding سرویس ها:
حال باید RoutingService را به صورت زیر هاست نماییم:
مهمترین بخش تنظیمات مربوط به Routing Service است:
WCF Routing Service چیست؟
Routing Service به عنوان سرویس مسیریابی WCF در دات نت 4 معرفی شد. به وسیله Routing Service میتوان Endpoint Configuration مقصدهای مختلف را با هم تجمیع کرد و در نتیجه تعداد تنظیمات برای Endpoint در سمت کلاینت کاهش پیدا میکند به طوری که کلاینت فقط با یک مقصد در ارتباط است. مقصد کلاینت همان Routing Service میباشد که در این سرور درخواستهای رسیده از کلاینتها با توجه به فیلتر انجام شده به مقصد اصلی ارسال خواهند شد.
با استفاده از Routing Service معماری سیستم به صورت تغییر پیدا میکند:
اهداف:
موارد زیر اهداف و مزایای استفاده از Routing Service است:
»Service versioning
»Content-based routing scenario
»Service partitioning
»Protocol bridging
هر کدام از موارد بالا در طی پستهای جداگانه شرح داده خواهند شد.
بررسی یک مثال:
دو Contract به صورت زیر تعریف میکنیم:
[ServiceContract] public interface ICalculatorV1 { [OperationContract] int Add(int a, int b); } [ServiceContract] public interface ICalculatorV2 { [OperationContract] int Sub(int a, int b); }
public class CalculatorV1 : ICalculatorV1 { public int Add(int a, int b) { return a + b; } } public class CalculatorV2 : ICalculatorV2 { public int Sub(int a, int b) { return a - b; } }
system.serviceModel> <services> <service name="WCFRoutingSample.CalculatorV1"> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/CalculatorServiceV1/" /> </baseAddresses> </host> <endpoint address ="" binding="basicHttpBinding" contract="WCFRoutingSample.ICalculatorV1"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> <service name="WCFRoutingSample.CalculatorV2"> <host> <baseAddresses> <add baseAddress = "http://localhost:8733/CalculatorServiceV2/" /> </baseAddresses> </host> <endpoint address ="" binding="basicHttpBinding" contract="WCFRoutingSample.ICalculatorV2"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
حال باید RoutingService را به صورت زیر هاست نماییم:
class Program { static void Main(string[] args) { var host = new ServiceHost(typeof(RoutingService)); host.Open(); Console.WriteLine("Server is running."); Console.ReadLine(); host.Close(); } }
مهمترین بخش تنظیمات مربوط به Routing Service است:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="routingBehv"> <routing routeOnHeadersOnly="false" filterTableName="filters"/> <serviceDebug includeExceptionDetailInFaults="true"/> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <routing> <filters> <filter name="CalV1ServiceFilter" filterType="EndpointName" filterData="Calv1Service"/> <filter name="CalV2ServiceFilter" filterType="EndpointName" filterData="Calv2Service"/> </filters> <filterTables> <filterTable name="filters"> <add filterName="CalV1ServiceFilter" endpointName="Calv1Service" /> <add filterName="CalV2ServiceFilter" endpointName="Calv2Service"/> </filterTable> </filterTables> </routing> <services> <!-- Routing service with endpoint definition --> <service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="routingBehv"> <endpoint address="/Calv1" binding="basicHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" name="Calv1Service"/> <endpoint address="/Calv2" binding="basicHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" name="Calv2Service"/> <host> <baseAddresses> <add baseAddress="http://localhost:9000/CalculatorService"/> </baseAddresses> </host> </service> </services> <client> <endpoint address="http://localhost:8732/CalculatorServiceV1" binding="basicHttpBinding" contract="*" name="Calv1Service"/> <endpoint address="http://localhost:8733/CalculatorServiceV2" binding="basicHttpBinding" contract="*" name="Calv2Service"/> </client> </system.serviceModel>
همان طور که مشاهده میکنید ابتدا اطلاعات Binding دو سرویس نوشته در بالا را به عنوان Endpointهای مختلف تعریف کردیم و سپس با استفاده از FilterTable نوع درخواست را به Endpoint مورد نظر وصل کردیم(در این مثال فیلتر بر اساس نوع Endpoint است). به وسیله این تعاریف Routing Service میداند که هر درخواست را به کدام سرویس ارسال نماید و پاسخ به کجا بازگشت داده شود.
نحوه نمایش مقدار Display attribute پراپرتیها در تگهای Label با استفاده از کامپوننتهای جنریک
در حال حاضر چنین قابلیتی به صورت توکار در Blazor وجود ندارد، برای اینکه این قابلیت رو با استفاده از کامپوننتها پیاده سازی کنیم میتوان از یک کامپوننت جنریک استفاده کرد (اطلاعات بیشتر در مورد کامپوننتهای جنریک):
همچنین میتوان ChildContent رو که یک RenderFragment است مقداری دهی کرد که در کنار Display، مقدار ChildContent رو هم نمایش بده.
برای اتریبیوت Display هم باید مقدار ResourceType مقدار دهی شود:
استفاده از InputAttributes در تگ Label:
الان میتونیم هر تعداد اتریبیوتی رو به کامپوننت پاس بدیم:
در ASP.NET Core اگه بخوایم Display یک پراپرتی رو نمایش بدیم به این صورت عمل میکنیم:
@model ProjectName.ViewModels.Identity.RegisterViewModel <label asp-for="PhoneNumber"></label>
@using System.Reflection @using System.Linq.Expressions @using System.ComponentModel.DataAnnotations @typeparam T @if (ChildContent is null) { <label>@Label</label> } else { <label> @Label @ChildContent </label> } @code { [Parameter, EditorRequired] public Expression<Func<T>> DisplayNameFor { get; set; } = default!; [Parameter] public RenderFragment? ChildContent { get; set; } private string Label => GetDisplayName(); private string GetDisplayName() { var expression = (MemberExpression)DisplayNameFor.Body; var value = expression.Member.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute; return value?.Name ?? expression.Member.Name; } }
نحوه استفاده:
private LoginDto Login { get; } = new();
<CustomDisplayName DisplayNameFor="@(() => Login.UserName)" />
<CustomDisplayName DisplayNameFor="@(() => Login.UserName)"> <span class="text-danger">(*)</span> </CustomDisplayName>
نحوه استفاده در پروژههای چند زبانه
کامپوننت فوق برای استفاده در پروژه چند زبانه باید به صورت زیر تغییر پیدا کنه:
@using System.Reflection @using System.Linq.Expressions; @using System.ComponentModel.DataAnnotations; @typeparam T @if (ChildContent == null) { <label>@Label</label> } else { <label> @Label @ChildContent </label> } @code { [Parameter] public Expression<Func<T>> DisplayNameFor { get; set; } = default!; [Parameter] public RenderFragment? ChildContent { get; set; } [Inject] public IStringLocalizerFactory LocalizerFactory { get; set; } = default!; private string Label => GetDisplayName(); private string GetDisplayName() { var expression = (MemberExpression)DisplayNameFor.Body; var displayAttribute = expression.Member.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute; if (displayAttribute is {ResourceType: not null }) { // Try to dynamically create an instance of the specified resource type var resourceType = displayAttribute.ResourceType; var localizer = LocalizerFactory.Create(resourceType); return localizer[displayAttribute.Name ?? expression.Member.Name]; } return displayAttribute?.Name ?? expression.Member.Name; } }
public class LoginDto { [Display(Name = nameof(LoginDtoResource.UserName), ResourceType = typeof(LoginDtoResource))] [Required] [MaxLength(100)] public string UserName { get; set; } = default!; //... }
LoginDtoResource یک فایل Resource است که باید با باز کردنش در ویژوال استودیو، Access modifier اونو به public تغییر بدید.
نحوه افزودن کلاس به Label ( ساده سازی تعاریف ویژگیهای المانها )
افزودن یک دیکشنری به کامپوننت:
[Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> InputAttributes { get; set; } = new();
@if (ChildContent is null) { <label @attributes="InputAttributes"> @Label </label> } else { <label @attributes="InputAttributes"> @Label @ChildContent </label> }
<CustomDisplayName DisplayNameFor="@(() => Login.UserName)" class="form-label" id="test" for="UserName" />
پاسخ به بازخوردهای پروژهها
تگ a در گزارش
نیازی نیست برای صرفا تبدیل HTML به PDF از کتابخانه PDFReport استفاده کنید. کتابخانه PdfReport برای قسمتهای تبدیل HTML به PDF خودش از HTMLWorker کتابخانه iTextSharp استفاده میکند.
اطلاعات بیشتر
ضمنا این کتابخانه مشکلی با لینکها هم ندارد. یک مثال:
پ.ن.
در هر برنامهای یک گزارش خطا زمان قابل رسیدگی خواهد بود که قابلیت تکرار مجدد داشته باشد به همراه ارائه کامل stack trace خطای دریافتی.
اطلاعات بیشتر
ضمنا این کتابخانه مشکلی با لینکها هم ندارد. یک مثال:
var html = @"<a color='blue' href='https://www.dntips.ir'>سایت دات نت</a>"; using (var pdfDoc = new Document(PageSize.A4)) { PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create)); pdfDoc.Open(); FontFactory.Register("c:\\windows\\fonts\\tahoma.ttf"); StyleSheet styles = new StyleSheet(); styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma"); styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, "Identity-H"); styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ALIGN, HtmlTags.ALIGN_LEFT); var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(html), styles); PdfPCell pdfCell = new PdfPCell { Border = 0 }; pdfCell.RunDirection = PdfWriter.RUN_DIRECTION_RTL; foreach (var htmlElement in parsedHtmlElements) { pdfCell.AddElement(htmlElement); } var table1 = new PdfPTable(1); table1.WidthPercentage = 100; table1.RunDirection = PdfWriter.RUN_DIRECTION_RTL; table1.AddCell(pdfCell); pdfDoc.Add(table1); }
پ.ن.
در هر برنامهای یک گزارش خطا زمان قابل رسیدگی خواهد بود که قابلیت تکرار مجدد داشته باشد به همراه ارائه کامل stack trace خطای دریافتی.
اشتراکها